demo1
package webdriverdemo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class webdriverdemo { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); //Puts a Implicit wait, Will wait for seconds before throwing exception driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); //打开百度登陆页面 driver.get("https://passport.baidu.com/v2/?login"); //找到用户名和密码文本框 WebElement user = driver.findElement(By.id("TANGRAM__PSP_3__userName")); WebElement pwa = driver.findElement(By.id("TANGRAM__PSP_3__password")); //分别将用户名和密码文本框清空 user.clear(); pwa.clear(); //输入用户名和密码 user.sendKeys("******"); pwa.sendKeys("******"); //找到登陆按钮点击 driver.findElement(By.id("TANGRAM__PSP_3__submit")).click(); //输出title System.out.println(driver.getTitle()); //Close the Browser. driver.close(); } }
demo2
package testNG; import org.testng.Assert; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class TestBMI { private WebDriver driver; @DataProvider public String[][] testData() { return new String[][] { { "180", "60", "18.5" }, { "180", "70", "21.6" },{ "170", "65", "22.5" }, { "160", "50", "19.5" }}; } @BeforeTest //启动FirefoxDriver并打开测试网页 public void setUp() { driver = new FirefoxDriver(); driver.get("http://cn.onlinebmicalculator.com/"); } /* System.setProperty("webdriver.chrome.driver", "./chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("http://cn.onlinebmicalculator.com/"); */ @Test(dataProvider = "testData") //开始数据驱动测试 public void testBMICalculator(String height, String weight,String expectedBMI) { // 输入身高 WebElement heightElement = driver.findElement(By.cssSelector("input[name='f_height']")); heightElement.clear(); heightElement.sendKeys(height); // 输入体重 WebElement weightElement = driver.findElement(By.cssSelector("input[name='f_weight']")); weightElement.clear(); weightElement.sendKeys(weight); // 点击计算按钮 WebElement computeElement = driver.findElement(By.cssSelector("input[name='f_submit']")); computeElement.click(); // 得到bmi元素的值 WebElement bmiElement = driver.findElement(By.className("bmi_info")).findElement(By.tagName("span")); String actualBMI = bmiElement.getText(); System.out.println("actualBMI:" + actualBMI); // 断言expectedBMI的值与actualBMI的值相等 Assert.assertEquals(expectedBMI, actualBMI); } @AfterTest //关闭浏览器 public void tearDown() { driver.quit(); } }
ADD
package educate; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class AddKuozhan { private static WebElement desc; private static WebElement value; public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); //打开登陆页面 driver.get("https://bumng.test.alipay.net/commonlogin/login.htm?goto=https%3A%2F%2Fbizprodmng.test.alipay.net%2Fvirtualprod%2FagentExtShow.htm%3FloginType%3D1"); //找到原有后台账号 driver.findElement(By.id("loginSwitch")).click(); //找到用户名和密码文本框 // user = driver.findElement(By.name("loginName")); // pwa = driver.findElement(By.name("bumngPassword")); // //分别将用户名和密码文本框清空 // user.clear(); // pwa.clear(); // //输入用户名和密码 // user.sendKeys("admin"); // pwa.sendKeys("888888"); driver.findElement(By.name("loginName")).sendKeys(new String[] { "admin" }); driver.findElement(By.name("bumngPassword")).sendKeys(new String[] { "888888" }); //找到登陆按钮点击 driver.findElement(By.cssSelector("button.ant-btn.ant-btn-primary")).click(); //输出title System.out.println("登陆成功"); //点击新增 driver.findElement(By.xpath("//input[@value='新增']")).click(); //定位学校下拉框 driver.findElement(By.xpath("//*[@id='agentForm']/div[1]/table/tbody/tr[1]/td[2]/span/span/a")).click(); driver.findElement(By.xpath("//div[@class='combobox-item'][text()='中国人民大学']")).click(); //定位属性名称 driver.findElement(By.xpath("//*[@id='agentForm']/div[1]/table/tbody/tr[1]/td[4]/span/span/a")).click(); driver.findElement(By.xpath("//div[@class='combobox-item'][text()='签名算法']")).click(); //值和描述输入框 value = driver.findElement(By.name("propertyValue")); desc = driver.findElement(By.name("propertyDesc")); //输入值和描述 // value.sendKeys("RSA"); // desc.sendKeys("签名算法"); value.sendKeys(new String[] { "RSA" }); desc.sendKeys(new String[] { "签名算法" }); //点击确定 driver.findElement(By.xpath("//input[@value='确 定']")).click(); System.out.println("新增属性成功"); } }
ADDS
package educate; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class AddKuozhanPiliang { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); //打开登陆页面 driver.get("https://bumng.test.alipay.net/commonlogin/login.htm?goto=https%3A%2F%2Fbizprodmng.test.alipay.net%2Fvirtualprod%2FagentExtShow.htm%3FloginType%3D1"); //找到原有后台账号 driver.findElement(By.id("loginSwitch")).click(); //找到用户名和密码文本框 // user = driver.findElement(By.name("loginName")); // pwa = driver.findElement(By.name("bumngPassword")); // //分别将用户名和密码文本框清空 // user.clear(); // pwa.clear(); // //输入用户名和密码 // user.sendKeys(admin); // pwa.sendKeys("888888"); driver.findElement(By.name("loginName")).sendKeys(new String[] { "admin" }); driver.findElement(By.name("bumngPassword")).sendKeys(new String[] { "888888" }); //找到登陆按钮点击 driver.findElement(By.cssSelector("button.ant-btn.ant-btn-primary")).click(); //输出title System.out.println("登陆成功"); //选择学校 driver.findElement(By.xpath("//div[@class='combobox-item'][text()='华中科技大学']")).click(); //点击批量新增 driver.findElement(By.xpath("//input[@value='批量新增']")).click(); //点击确定 driver.findElement(By.xpath("//input[@value='确 定']")).click(); System.out.println("新增属性成功"); } }
Login
package educate; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class Login { public static void main(String[] args) { // System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); WebDriver driver = new FirefoxDriver(); //打开登陆页面 driver.get("https://bumng.test.alipay.net/commonlogin/login.htm?goto=https%3A%2F%2Fbizprodmng.test.alipay.net%2Fvirtualprod%2FagentExtShow.htm%3FloginType%3D1"); //找到原有后台账号 driver.findElement(By.id("loginSwitch")).click(); //找到用户名和密码文本框 //WebElement user = driver.findElement(By.name("loginName")); //WebElement pwa = driver.findElement(By.name("bumngPassword")); //分别将用户名和密码文本框清空 //user.clear(); //pwa.clear(); //输入用户名和密码 //user.sendKeys("admin"); //pwa.sendKeys("888888"); driver.findElement(By.name("loginName")).sendKeys(new String[] { "admin" }); driver.findElement(By.name("bumngPassword")).sendKeys(new String[] { "888888" }); //找到登陆按钮点击 driver.findElement(By.cssSelector("button.ant-btn.ant-btn-primary")).click(); //注意:使用className来进行元素定位时,有时会碰到一个元素指定了若干个class属性值的“复合样式”的情况,如下面这个button: //<button id="J_sidebar_login" class="btn btn_big btn_submit" type="submit">登录</button>。 //这个button元素指定了三个不同的css伪类名作为它的样式属性值,此时就必须结合后面要介绍的cssSelector方法来定位了,稍后会有详细例子。 //输出title System.out.println(driver.getTitle()); } //Close the Browser. //driver.close(); }
查询
package educate; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class QueryKuozhan { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); //打开登陆页面 driver.get("https://bumng.test.alipay.net/commonlogin/login.htm?goto=https%3A%2F%2Fbizprodmng.test.alipay.net%2Fvirtualprod%2FagentExtShow.htm%3FloginType%3D1"); //找到原有后台账号 driver.findElement(By.id("loginSwitch")).click(); //找到用户名和密码文本框 driver.findElement(By.name("loginName")).sendKeys(new String[] { "admin" }); driver.findElement(By.name("bumngPassword")).sendKeys(new String[] { "888888" }); //找到登陆按钮点击 //driver.findElement(By.cssSelector("button.ant-btn.ant-btn-primary")).click(); driver.findElement(By.xpath("//*[@id='login-form']/div[3]/button")).click(); //输出title System.out.println(driver.getTitle()); //定位学校下拉框 driver.findElement(By.cssSelector("span.textbox-addon.textbox-addon-right")).click(); //选择学校 //driver.findElement(By.id("_easyui_combobox_i2_3")).click(); driver.findElement(By.xpath("//div[@class='combobox-item'][text()='华中科技大学']")).click(); //点击查询 driver.findElement(By.xpath("//input[@type='submit'][@value='查询']")).click(); System.out.println("机构扩展信息查询成功"); } }