selenium 学习(7)--webdriver

上一篇 / 下一篇  2014-07-25 15:41:12

首先看看《开源应用架构(The Architecture of Open Source Applications)http://www.infoq.com/cn/news/2011/06/selenium-arch 中有关webdriver的介绍

 从 较高的层次看,Selenium由三种工具组成。第一个工具Selenium IDE,是Firefox的扩展插件,支持用户录制和回访测试。录制/回访模式?存在局限性,对许多用户来说并不适合,因此第二个工具—— Selenium WebDriver提供了各种语言环境的API来支持更多控制权和编写符合标准软件开发实践的应用程序。最后一个工具——Selenium Grid帮助工程师使用Selenium API控制分布在一系列机器上的浏览器实例,支持并发运行更多测试。在项目内部,它们分别被称为“IDE”、“WebDriver”和“Grid”。
   而现在我们大家能看到的selenium2.0 =selenium1.0+ webdriver

---------------------以上是要事先弄明白的问题,不然还以为webdriver是selenium的一个辅助工具呢(我当初就这么想的)

什么是WebDriver.

 WebDriver is a tool for automating testing web applications, and in particular to verify that they work as expected. It aims to provide a friendly API that’s easy to explore and understand, easier to use than the Selenium-RC (1.0) API, which will help make your tests easier to read and maintain. It’s not tied to any particular test framework, so it can be used equally well in a unit testing or from a plain old “main” method.
   webDriver 不与任何测试框架绑定,可以和junit/ TestNG或者其他平台一起工作。(甚至我想,完全可以把它当作一个独立的工具,和selenium没啥关系的工具)

什么时候用webDriver,用webDriver还要不要selenium server了呢?

在以下的情况用webDriver:

  • 1. 多浏览器测试,selenium中不支持的浏览器
  • 2. 多框架,多浏览器窗口,弹出框,警告窗口
  • 3. 页面导航
  • 4. 拖动
  • 5. 基因Ajax的UI元素。

有了webDriver还要不要selenium server呢?或许在某种情况下你更本就用启动server    

 You may, or may not, need the Selenium Server, depending on how you intend to use Selenium. If you will be strictly using the WebDriver API you do not need the Selenium Server. The Selenium Server provides Selenium-RC functionality, which is primarily used for Selenium 1.0 backwards compatability. Since WebDriver uses completely different technology to interact with the browsers, the Selenium Server is not needed. Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. Selenium-RC however requires the Selenium- Server to inject javascript. into the browser and to then translate messages from your test program’s language-specific Selenium client library into commands that invoke the javascript. commands which in turn, automate the AUT from within the browser. In short, if you’re using Selenium-WebDriver, you don’t need the Selenium-Server.

Another reason for using the Selenium-Server is if you are using Selenium-Grid for distributed exectution of your tests. Finally, if you are using Selenium-backed Web-Driver (the WebDriver API but with back-end Selenium technology) you will also need the Selenium Server. These topics are described in more detail later in this chapter.

   用不用server完全取决于你怎么用selenium。

      1. 如果你严格只有webDriver的API,那么就不用启动server。

      2. 如果你用Selenium-Grid来分布式执行你的用例,那么就要用server。

      3. 如果你用的webDriver的API,但这个API又用了selenium的某些基层的技术,那么还是要启动server

 

WebDriver的几种实现以及区别

  特点 执行速度
 HtmlUnit Driver 
  • Fastest implementation of WebDriver
  • A pure Java solution and so it is platform. independent.
  • Supports JavaScript.
 
  • Emulates other browsers’ JavaScript. behaviour
  •  Firefox Driver  
  • Slower than theHtmlUnit Driver
  •  IE Driver 
  •   Runs in a real browser and supports JavaScript.
  •  
     Chrome Driver 
    • Runs in a real browser and supports JavaScript.
    • Because Chrome is a Webkit-based browser, theChrome Drivermay allow you to verify that your site works in Safari. Note that since Chrome uses its own V8 JavaScript. engine rather than Safari’s Nitro engine, JavaScript. execution may differ.
     
  • Slower than theHtmlUnit Driver
  •    

    java 版本的webDriver实践

     

    packageorg.openqa.selenium.example;

    importorg.openqa.selenium.By;
    importorg.openqa.selenium.WebDriver;
    importorg.openqa.selenium.WebElement;
    importorg.openqa.selenium.firefox.FirefoxDriver;
    importorg.openqa.selenium.support.ui.ExpectedCondition;
    importorg.openqa.selenium.support.ui.WebDriverWait;

    publicclassSelenium2Example {
       publicstaticvoidmain(String[]args){
           // Create a new instance of the Firefox driver
           // Notice that the remainder of the code relies on the interface,
           // not the implementation.
           WebDriverdriver=newFirefoxDriver();

           // And now use this to visit Google
           driver.get("http://www.google.com");
           // Alternatively the same thing can be done like this
           // driver.navigate().to("http://www.google.com");

           // Find the text input element by its name
           WebElementelement=driver.findElement(By.name("q"));

           // Enter something to search for
           element.sendKeys("Cheese!");

           // Now submit the form. WebDriver will find the form. for us from the element
           element.submit();

           // Check the title of the page
           System.out.println("Page title is: "+driver.getTitle());
           
           // Google's search is rendered dynamically with JavaScript.
           // Wait for the page to load, timeout after 10 seconds
           (newWebDriverWait(driver,10)).until(newExpectedCondition<Boolean>(){
               publicBooleanapply(WebDriverd){
                   returnd.getTitle().startsWith("cheese!");
               }
           });

           // Should see: "cheese! - Google Search"
           System.out.println("Page title is: "+driver.getTitle());
           
           //Close the browser
           driver.quit();
       }
    }
       

    TAG:

     

    评分:0

    我来说两句

    Open Toolbar