demo1css
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(); } }
demo2java
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(); } }
ADDweb
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("新增屬性成功"); } }
Loginchrome
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("機構擴展信息查詢成功"); } }