Java+Selenium Web UI自動化測試的一些小總結

來源:互聯網
上載者:User

標籤:linux   size   int   error   into   測試   java   認證   sel   

- Chrome

關於Chrome的配置網上資訊很多,直說一點,當選擇在linux環境跑用例的時候,我們會優先選擇headless run,預設情況下headless run的瀏覽器大小好像只有900*768,我們在windows下調通的用例在縮小的串口下經常會失敗,最好調一下size:

chromeOptions.setHeadless(true);chromeOptions.addArguments("--no-sandbox");chromeOptions.addArguments("window-size=1280,900");

- Firefox,

a. Chrome下能夠跑過的用例在firefox下可能會fail,原因之一是Firefox對所有超出視窗的元素都是不可操作的,即使使用Action或者JS來操作也不可行,需要添加相應的ScrollIntoView (下面click會介紹具體用法)

b. Firefox對隱藏元素預設是不可操作的(例如上傳檔案標籤),firefox 59之後添加了FirefoxCapability moz:wedriverClick用於操作隱藏元素: firefoxOptions.setCapability("moz:webdriverClick",false); //點擊隱藏元素,如上傳檔案

2. 關於Click

- Element.click

driver.findElement(By.css).click(); //前提條件為element可見(visible)且高度長度大於0,如果元素點擊時發生變化則會拋出(StaleElementReferenceError

- Action.click

Actions action = new Actions(driver);action.moveToElement(element).click().perform();//類比滑鼠操作,點擊元素中間位置

- Javascript scrollIntoView and click

JavascriptExecutorje=(JavascriptExecutor)getWebDriver();je.executeScript("arguments[0].scrollIntoView(false);",element);//移動到元素element對象的“底端”與當前視窗的“底部”對齊,//true為頂端je.executeScript("arguments[0].click();",element);//通過JS點擊元素,可繞開元素被圖層覆蓋或透明沒有正面大小問題

  

3. Driver

- Chromedriver:

○ Headless mode: (Chrome headless模式時不使用Xvfb,Xvfb為早期沒有chrome headless模式時的替代X window服務, headless預設視窗大小為900x768,可自行設定)

chromeOptions.setHeadless(true);chromeOptions.addArguments("--no-sandbox");chromeOptions.addArguments("window-size=1280,900");

○ Linux/Mac環境下運行chromedriver需要強制設定其為可執行檔

Filefile=newFile("chromedriver path");file.setExecutable(true);

- Geckodriver

ProfilesIniprofile=newProfilesIni();FirefoxProfileffProfile=profile.getProfile("default");capabilities.setCapability(FirefoxDriver.PROFILE,ffProfile); //關於認證認證等的設定FirefoxOptionsfirefoxOptions=newFirefoxOptions(capabilities);firefoxOptions.setCapability("moz:webdriverClick",false);//允許點擊隱藏元素,如上傳檔案webdriver=newFirefoxDriver(firefoxOptions);

4. Hover

- Hover and click

Actionsaction=newActions(getWebDriver());action.moveToElement(element).perform();Element.click();//如果出現點擊不了的問題,可嘗試更改moveToElement到父節點,點擊子element

  

- Hover and hold

Actionsaction=newActions(getWebDriver());action.moveToElement(element).clickAndHold().perform();//側面解決hover元素後在做其他動作hover元素隱藏問題action.release();

  

後續慢慢添加

Java+Selenium Web UI自動化測試的一些小總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.