Java+Selenium Web UI自动化测试的一些总结

1. Browser

Chrome

关于Chrome的配置网上信息不少,直说一点,当选择在linux环境跑用例的时候,咱们会优先选择headless run,默认状况下headless run的浏览器大小好像只有900*768,咱们在windows下调通的用例在缩小的串口下常常会失败,最好调一下size:css

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

Firefox

a. Chrome下可以跑过的用例在firefox下可能会fail,缘由之一是Firefox对全部超出视窗的元素都是不可操做的,即便使用Action或者JS来操做也不可行,须要添加相应的ScrollIntoView (下面click会介绍具体用法)java

b. Firefox对隐藏元素默认是不可操做的(例如上传文件标签),firefox 59以后添加了FirefoxCapability moz:wedriverClick用于操做隐藏元素: firefoxOptions.setCapability("moz:webdriverClick",false); //点击隐藏元素,如上传文件linux

2. 关于Click

- Element.clickweb

driver.findElement(By.css).click(); //前提条件为element可见(visible)且高度长度大于0,若是元素点击时发生变化则会抛出(StaleElementReferenceError

- Action.clickchrome

Actions action = new Actions(driver);
action.moveToElement(element).click().perform();//模拟鼠标操做,点击元素中间位置

- Javascript scrollIntoView and clickwindows

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,可自行设置)less

chromeOptions.setHeadless(true);

chromeOptions.addArguments("--no-sandbox");

chromeOptions.addArguments("window-size=1280,900");

○ Linux/Mac环境下运行chromedriver须要强制设置其为可执行文件spa

Filefile=newFile("chromedriver path");

file.setExecutable(true);

- Geckodriverfirefox

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();

  

后续慢慢添加

相关文章
相关标签/搜索