webdriver常用技巧(java)

场景一:鼠标滑动
element = driver.findElement(By. cssSelector(".user-info.right>div>p>a"));
Actions builder =  new Actions(driver);
builder.moveToElement(element).build().perform();
//Thread.sleep(100); no need
element = driver.findElement(By. cssSelector(".frd-del"));
element.click();

场景二:加载ua
private static WebDriver  driver;
FirefoxProfile profile =  new FirefoxProfile();
profile.setPreference("general.useragent.override",
      "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16");
driver =  new FirefoxDriver(profile);

场景三:页面切换
String pWindUrl =  driver.getCurrentUrl();
//跳至新打开的窗口
if ( driver.getWindowHandles().size() != 2) { fail(); }
for (String handle :  driver.getWindowHandles()) {
     driver.switchTo().window(handle);
     if ( driver.getCurrentUrl().equals(pWindUrl)){
       driver.close(); //关闭父窗口
    }  else {
            //6张图片
             if ( driver.findElements(By. cssSelector(".img")).size() != 6) {  fail(); }
    }
}

场景四:alert提示
场景五:截屏
private Alert alert;
alert = driver.switchTo().alert();
alert.accept();
//alert.dismiss();

场景五:截屏
try {
      File scrFile = ((TakesScreenshot) driver)
                  .getScreenshotAs(OutputType. FILE);
      FileUtils. copyFile(scrFile,  new File("img\\Screenshot.png"));
catch (Exception e) {
      e.printStackTrace();
finally {
      // driver.close();
}

你可能感兴趣的:(java,exception,String,OS,File,iPhone)