selenium技巧——通过js来控制滚动条

来源:互联网 发布:云计算的四种部署模式 编辑:程序博客网 时间:2024/05/18 03:02
业务流程:
  1.打开此网页 http://nanjing.xiaomishu.com/shop/search/sp2048_745
  2.向下拖动滚动条,右下角自动会出现【投诉与建议】(此网页已经修改不拖动也出现了,以前是没有的,)
  3.点击【投诉与建议】
  4.在打开的div 层中输入姓名,邮件,内容 并点击确定
  5.验证页面上的提示文字
 
package com.example.tests;import org.junit.*;import org.openqa.selenium.*;import org.openqa.selenium.ie.InternetExplorerDriver;public class SeleniumWebDriver { public static WebDriver driver;  @Test public void testUnit() {  driver = new InternetExplorerDriver();  driver.get(http://nanjing.xiaomishu.com/shop/search/sp2048_745);    maxBrowser(driver);  setScroll(driver,500);  driver.findElement(By.linkText("投诉与建议")).click();   driver.findElement(By.xpath("//input[@id='repName']")).sendKeys("1");  driver.findElement(By.xpath("//input[@id='repMail']")).sendKeys("1");  driver.findElement(By.xpath("//textarea[@id='repContent']")).sendKeys("hello");  driver.findElement(By.xpath("//a[@id='repBtn']")).click();    Assert.assertEquals("您输入的邮箱格式不正确", driver.findElement(By.xpath("//div[@id='floatBox_remind']/span")).getText());   }   //将IE最大化  public static void  maxBrowser(WebDriver driver){   try {    String maxBroswer = "if (window.screen) {window.moveTo(0, 0);" +      "window.resizeTo(window.screen.availWidth,window.screen.availHeight);}";        JavascriptExecutor jse=(JavascriptExecutor) driver;    jse.executeScript(maxBroswer);   } catch (Exception e) {    System.out.println("Fail to  Maximization browser");   }  }  //将滚动条滚到适合的位置 public static void setScroll(WebDriver driver,int height){  try {   String setscroll = "document.documentElement.scrollTop=" + height;      JavascriptExecutor jse=(JavascriptExecutor) driver;   jse.executeScript(setscroll);  } catch (Exception e) {   System.out.println("Fail to set the scroll.");  }    } }

0 0
原创粉丝点击