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

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

  

后续慢慢添加

猜你喜欢

转载自www.cnblogs.com/testerLydia/p/9463927.html
今日推荐