【原创】零基础学习Selenium自动化测试(三)--WebDriver对浏览器的操作

WebDriver对浏览器的操作

1、初始化firefox浏览器

WebDriver driver = new FirefoxDriver();

2、初始化ie浏览器

WebDriver driver = new InternetExplorerDriver();

3、初始化chrome浏览器

WebDriver driver = new ChromeDriver();

4、访问一个指定的URL

/**
   * Load a new web page in the current browser window. This is done using an HTTP GET operation,
   * and the method will block until the load is complete. This will follow redirects issued either
   * by the server or as a meta-redirect from within the returned HTML. Should a meta-redirect
   * "rest" for any duration of time, it is best to wait until this timeout is over, since should
   * the underlying page change whilst your test is executing the results of future calls against
   * this interface will be against the freshly loaded page. Synonym for
   * {@link org.openqa.selenium.WebDriver.Navigation#to(String)}.
   *
   * @param url The URL to load. It is best to use a fully qualified URL
   */
driver.get("http://www.baidu.com/");//访问百度首页

5、关闭浏览器

  /**
   * Close the current window, quitting the browser if it's the last window currently open.
   */
driver.close();//关闭当前页面
  /**
   * Quits this driver, closing every associated window.
   */
driver.quit();//关闭全部由driver启动的页面

6、返回当前浏览器窗口的URL(一般作为验证点)

/**
   * Get a string representing the current URL that the browser is looking at.
   *
   * @return The URL of the page currently loaded in the browser
   */
String currentUrl = driver.getCurrentUrl();

7、返回当前浏览器窗口的Title(一般作为验证点)

  /**
   * The title of the current page.
   *
   * @return The title of the current page, with leading and trailing whitespace stripped, or null
   *         if one is not already set
   */
String currentTitle = driver.getTitle();

8、一些导航操作

driver.navigate().back();// 向前
driver.navigate().forward();// 向后
driver.navigate().refresh();// 当前页刷新
driver.manage().window().maximize();// 浏览器窗口最大化

9、下一篇介绍Selenium对火狐浏览器的一些特殊处理


-------------------------------------我是华丽的分割线--------------------------------------------------------
Testfan楼大管家原创Selenium系列学习文章列表,点击阅读
---------------------------------------------------------------------------------------------------------------------


  • 发表于 2016-07-14 12:35
  • 阅读 ( 1537 )

0 条评论

请先 登录 后评论
不写代码的码农
楼大管家

测试达人

24 篇文章

作家榜 »

  1. hey girl 181 文章
  2. 王前前 137 文章
  3. 柠檬 118 文章
  4. lamking 105 文章
  5. 云少 105 文章
  6. 腾讯移动品质中心TMQ 104 文章
  7. humortony 82 文章
  8. 开心的阳光 41 文章