Selenium WebDriver学习笔记一

Selenium是web自动化测试框架,从2.0版本后加入了webdriver功能, 可以用java,python等主流语言完全操控浏览器进行自动化测试,功能非常强大。

下面演示一个很简单的例子,就是通过webdriver打开一个浏览器,然后访问google,搜索一个字符串,再显示结果页面的title。例子都是从selenium文档里抄的,语言为java。

  1. 安装jdkmaven(记得设置JAVA_HOME, M2_HOME宏)
  2. 在firefox安装selenium的插件
  3. 新建工程目录,创建pom.xml,内容如下
    
    
            4.0.0
            MySel20Proj
            MySel20Proj
            1.0
            
                UTF-8
            
            
                
                    org.seleniumhq.selenium
                    selenium-java
                    2.37.0
                
                
                    com.opera
                    operadriver
                
            
            
                
                    
                        com.opera
                        operadriver
                        1.5
                        
                            
                                org.seleniumhq.selenium
                                selenium-remote-driver
                            
                        
                    
                
            
    
    
    
  4. 在目录里执行: mvn clean install
  5. 在Idea里import新生成的maven project
  6. 创建子目录 src\main\java\org.openqa.selenium
  7. 在子目录下创建文件Selenium2Example.java, 内容如下
    package org.openqa.selenium.example;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Selenium2Example  {
        static WebDriver driver ;
    
        public static void main(String[] args) {
            // Create a new instance of the Firefox driver
            // Notice that the remainder of the code relies on the interface,
            // not the implementation.
            driver = new FirefoxDriver();
    
            // 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
            WebElement element = 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
            (new WebDriverWait(driver, 10)).until(new ExpectedCondition() {
                public Boolean apply(WebDriver d) {
                    return d.getTitle().toLowerCase().startsWith("cheese!");
                }
            });
    
            // Should see: "cheese! - Google Search"
            System.out.println("Page title is: " + driver.getTitle());
    
            //Close the browser
            driver.quit();
        }
    }
  8. 在项目目录执行如下命令:
    mvn compile
    mvn exec:java -Dexec.mainClass=”org.openqa.selenium.example.Selenium2Example”

    运行后就能看到一个firefox窗口被创建出来,自动访问google并搜索, 在console窗口中会看到title的输出