Selenium手册(二)——WebDrive

发布时间:2020-06-14 21:45:02 作者:pocp
来源:网络 阅读:1115

【2016/8/2 9:00 更新】

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>MySel20Proj</groupId>
        <artifactId>MySel20Proj</artifactId>
        <version>1.0</version>
        <dependencies>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>2.53.1</version>
            </dependency>
        </dependencies>
</project>


        以上内容中的版本信息为此文档编辑时的最新版本信息,实际操作中请以当下最新版本信息为主。

       
    【个人笔记】
     学习过Maven入门视频(一)的人对Selenium给出的pom.xml的范例就能大致理解了。上文提供的最新版本信息,指的是“dependencies”下的version,要查询最新version,直接在搜索引擎上输入artifactId项的文字加version,就能找到相关网站查询最新版本信息。

   “dependencies”下可添加多个“dependency”,保存完毕执行命令如“mvn clean install”,Maven就为你自行下载对应的JAR包。

    

        【结束】   

        3. 现在可以通过命令行窗口,CD进入工程文件夹,输入指令:mvn clean install,Maven将自动下载Selenium和所有项目依赖JAR包到文件夹中。最后,把工程导入IDE中,就可以开始编程了。


【个人笔记】

建立一个项目实战步骤:


1. 安装好Maven,复制setting.xml到"C:\Users\Administrator\.m2"下。

2. cd到你想放置工程的路径下,输入: mvn archetype:generate -DgroupId=MySel20Proj -    

            DartifactId=MySel20Proj -Dversion=1.0。Build succes后检查路径下的文件是否正确建立。

3. 打开pom.xml,Dependencies下Maven已经为我们下载了junit,junit是在单元测试中使用的包。参照

            Selenium官网提供的范例增加dependency。

        注意我们使用的语言是Java:

<dependencies>
     <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
     </dependency>
     <dependency>
           <groupId>org.seleniumhq.selenium</groupId>
           <artifactId>selenium-java</artifactId>
           <version>2.16.1</version>
     </dependency>
</dependencies>



4.  cd到pom.xml所在的路径下,执行mvn eclipse:clean(这一步会删除已存在的classpath和project文件),再执行mvn eclipse:eclipse,你可以看到pom.xml所以的路径下生成了classpath文件。然后打开eclipse,File->Import->Maven->Existing Maven project->选择路径,就可以看到新建的工程->Finish。



        注意:使用ChromeDriver,Opera Driver,Android Driver或iOS Driver需要再增加相应的操作。


        现在你已经可以开始编程了,先用这个范例练练手吧。此范例通过谷歌搜索“Cheese”并打印页面的标题。


 

package MySel20Proj;
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
{
       public static void main(String[] args)
      {
             //【个人笔记】如果程序报错找不到路径:
             //System.setProperty( "WebDriver.Firefox.bin", "找到Firefox.exe的安装路径,复制路径");
             //Syste.setProperty("WebDriver.chromedriver.exe", "同上");
             
             // 创建一个 Firefox driver的实例
             // 注意,接下来的代码依赖于接口(即WebDriver)
             // 而不是实现(即FirefoxDriver)
             // 注:FirefoxDriver implements WebDriver
            WebDriver driver = new FirefoxDriver();
            
             // 访问谷歌【打不开谷歌就换百度】
             driver .get("http://www.google.com" );
             // 你也可以用以下语句实现访问动作
             // driver.navigate().to("
              
             // 通过name来定位搜索框元素【百度 By.name("wd")】
            WebElement element = driver .findElement(By.name ("q" ));
            
             // 搜索cheese!
             element .sendKeys("Cheese!" );
             
             // 提交. WebDriver会为我们执行搜索动作
             element .submit();
             
             // 确认下页面title
            System. out .println("Page title is: " + driver.getTitle());
            
             // 谷歌【百度】的搜索是使用JavaScript进行动态呈现
             // 等待页面的加载,倒计时十秒
            ( new WebDriverWait(driver , 10)).until(
                         new ExpectedCondition<Boolean>()
                        {
                               public Boolean apply(WebDriver d )
                              {
                                     return d .getTitle().toLowerCase().startsWith( "cheese!");
                              }
                        });
                        
             // 打印输出""
            System. out .println("Page title is: " + driver.getTitle());
            
             //关闭浏览器
             driver .quit();
      }
}


        【个人笔记】

         我选择ChromeDriver实现段代码, 运行时,eclipse console窗口总是出现一行文字“Only local connections  are allowed”,这句话是什么意思?查一下API文档,ChromeDriver的定义:

          1. A WebDriver implementation that controls a Chrome browser running on the local machine.

          2. The control server which each instance communicates with will live and die with the instance.

          也就是说,ChromeDriver在本机生成一个server,server通过本机的一个开放端口进行监听,收集上层的请求,操控本机的chrome浏览器。那当然就是“只能允许本地连接”了。根据stackoverflow最高票的回答,这句话只是一句提示性的信息而已。

http://stackoverflow.com/questions/25080500/when-run-webdriver-with-chrome-browser-getting-message-only-local-connection

          












推荐阅读:
  1. UNIX/Linux 系统管理技术手册阅读(二)
  2. mongodb 英文手册

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

selenium 自动化测试工具 sel

上一篇:苹果Iphone/Ipad--L2TP虚拟教程

下一篇:吴明阳-全网运营技术高级讲师

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》