WebDriver高級應用javascript
public class Demo4 { WebDriver driver; // @BeforeMethod:在每一個測試方法開始運行前執行 @BeforeMethod public void beforeMethod(){ System.setProperty("webdriver.chrome.driver", "e:\\chromedriver.exe"); driver = new ChromeDriver(); } // @AfterMethod:在每一個測試方法開始運行結束後執行 @AfterMethod public void afterMethod(){ driver.close(); } /** * 使用JavaScriptExecutoru單擊元素 */ @Test public void testDame(){ driver.get("http://www.baidu.com"); //找到百度首頁的搜索輸入框 WebElement searchInbox = driver.findElement(By.id("kw")); //找到搜索按 WebElement searchButton = driver.findElement(By.id("su")); //在輸入框中輸入字符 searchInbox.sendKeys("selenium"); //調用javaScriptClick方法單擊按鈕 javaScriptClick(searchButton); } public void javaScriptClick(WebElement element){ try { //判斷傳入的element元素是否爲可單擊狀態,是否可見 if(element.isEnabled() && element.isDisplayed()){ System.out.println("使用javascript進行頁面元素的單擊"); ((JavascriptExecutor) driver).executeScript("arguments[0].click();",element); }else { System.out.println("頁面上的元素沒法進行單擊操做"); } } catch (NoSuchElementException e1) { System.out.println("頁面元素沒有附加在網頁中"); e1.printStackTrace(); } catch (Exception e2) { System.out.println("沒法完成單擊操做"); e2.printStackTrace(); } } /** * 在Ajax方式產生的浮動框中,選擇單擊包含某些關鍵字的選項 * @throws InterruptedException */ @Test public void testAjaxDemo() throws InterruptedException{ driver.get("https://www.sogou.com"); //找到搜索框 WebElement searthInbox = driver.findElement(By.id("query")); searthInbox.click(); Thread.sleep(2000); //將浮動的列表選項存list集合中 List<WebElement> webElements = driver.findElements(By.xpath("//*[@id='vl']/div[1]/ul/li")); for(WebElement webElement : webElements){ //判斷是否包含關鍵字 if(webElement.getText().contains("5月新規")){ System.out.println("............. "+ webElement.getText()); webElement.click(); Thread.sleep(3000); } } } //設置一個頁面對象的屬性值 @Test public void testDemo() throws InterruptedException{ driver.get("file:///F:/workspace/selenium_demo/src/main/webapp/text.html"); //獲取到id爲text的元素對象 WebElement textElement = driver.findElement(By.id("text_test")); //調用setAttribute方法修改文本框的value屬性值,改變文本框中的文字 setAttribute(driver,textElement,"value","selenium"); //修改文本框的size屬性值 setAttribute(driver, textElement, "size", "5"); Thread.sleep(2000); //刪除文本框的size屬性值 removeAttribute(driver, textElement, "size"); Thread.sleep(3000); } //修改頁面元素屬性的方法 private void setAttribute(WebDriver driver, WebElement textElement, String attributeName, String value) { JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", textElement,attributeName,value); } //刪除頁面元素屬性的方法 private void removeAttribute(WebDriver driver, WebElement textElement, String attributeName) { JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].removeAttribute(arguments[1])",textElement,attributeName); } /** * 在日期選擇器上進行日期選擇 */ @Test public void datepickerTest(){ driver.get("file:///F:/workspace/selenium_demo/src/main/webapp/text.html"); //獲取到日期控制元素對象 WebElement datepicker = driver.findElement(By.xpath("//*[@id='d12']")); //datepicker.click(); datepicker.sendKeys("2019-04-29"); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 高亮顯示正在被操做的頁面元素 */ @Test public void highLightTest(){ driver.get("http://www.baidu.com"); WebElement searchInbox = driver.findElement(By.id("kw")); WebElement submitButton = driver.findElement(By.id("su")); //調用高亮處理方法 highLightElenment(searchInbox); searchInbox.sendKeys("selenium"); highLightElenment(submitButton); submitButton.click(); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //高亮處理元素對象 private void highLightElenment(WebElement element) { JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].setAttribute('style',arguments[1])", element,"background:yellow;solid:red;"); } /** * 用例執行失敗對屏幕進行截屏 */ public void screenTest(){ } }
/** * 自動下載文件 * @author Administrator * */ public class DownloadFiles { //指定文件下載地址 static String downloadFilePath = "D:/download"; WebDriver driver; JavascriptExecutor js; String url; @BeforeMethod public void beforeMethod(){ url = "http://ftp.mozilla.org/pub/firefox/releases/35.0b8/win32/zh-CN/"; } /* @AfterMethod public void afterMethod(){ driver.quit(); }*/ @Test public void download(){ ChromeOptions options = downloadChromeFile();//更改默認下載路徑 driver = new ChromeDriver(options); driver.get(url); driver.findElement(By.partialLinkText("Stub")).click(); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } public static ChromeOptions downloadChromeFile(){ ChromeOptions options = new ChromeOptions(); //指定下載路徑 Map<String, Object> map = new HashMap<String, Object>(); map.put("profile.default_content_settings.popups", 0); map.put("download.default_directory", downloadFilePath); options.setExperimentalOption("prefs", map); return options; } }
public class Upload { @Test public void test(){ System.setProperty("webdriver.chrome.driver", "e:\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("file:///F:/workspace/selenium_demo/src/main/webapp/upload.html"); driver.findElement(By.name("uploadFile")).sendKeys("C:\\Users\\Administrator\\Desktop\\IDEA快捷鍵.txt"); } }
/** * 滑動滾動條到屏幕中間 * 滑動滾動條到指定元素位置 * 按橫縱坐票滑動屏幕 * @author Administrator * */ public class ScrollBar { WebDriver driver; String url; @BeforeMethod public void beforeMethod(){ url="http://v.sogou.com"; System.setProperty("webdriver.chrome.driver", "e:\\chromedriver.exe"); driver = new ChromeDriver(); driver.get(url); } @AfterMethod public void afterMethod(){ driver.quit(); } //第一優先級執行測試用例 @Test(priority=1) public void scrollBarToPage(){ //將頁面的滾動條滑動到屏幕的最下方 ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)"); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //第二優先級執行測試用例 @Test(priority=2) public void scrollBarToElemneTOfoPage(){ //將頁面的滾動條滑動到頁面指定的元素位置 ... 電視劇欄位置 WebElement element = driver.findElement(By.xpath("//*[@id='container']/div[2]/div[2]/div[2]/div[1]/h3/a")); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //第三優先級執行測試用例 @Test(priority=3) public void position(){ //按模縱座標滑動屏幕 ((JavascriptExecutor) driver).executeScript("window.scrollBy (0,800)"); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }