51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2447|回复: 2
打印 上一主题 下一主题

[转贴] JavaScript(Node.js)+ Selenium自动化测试

[复制链接]
  • TA的每日心情
    无聊
    昨天 09:20
  • 签到天数: 937 天

    连续签到: 4 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2016-10-8 16:24:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    Selenium is a browser automation library. Most often used for testing web-applications, Selenium may be used for any task that requires automating interaction with the browser.
      Selenium是一个浏览器自动化测试库,大多时候我们用它来测试web应用,Selenium 可以胜任任何在浏览器上自动化测试的任务。
      众所周知,Selenium可以支持多种编程语言(Java/ruby/python/C#/Go/JavaScipt),这篇博客就来介绍如何通过JavaScipt语言编写Selenium自动化测试脚本。在此之前,需要把环境搭建起来。
      之前有个问题一直弄不明白,JavaScipt脚本不是只打开浏览器才能执行么?如何运行Selenium呢?难道我要打开一个浏览器执行JavaScipt去驱动另一个浏览器执行?直到我昨天看了一点Node.js的资料,才突然明白。
      所以,需要先安装Node.js。
      官方网址:https://nodejs.org/en/


    Installation
      Selenium may be installed via npm with
      Selenium可以通过npm安装。(npm是随同NodeJS一起安装的包管理工具。)
    1. >npm install selenium-webdriver
    复制代码
    NOTE: Mozilla's geckodriver is only required for Firefox 47+. Everything you need for Firefox 38-46 is included with this package.
      Selenium官方在推出了3.0,值得庆祝,万年的2.x终于升级到3.0了,当然,3.0的正式版还没推出。其中带来了一些改变。最大的变化之一是,Firefox浏览器的驱动由原来集成在Selenium安装包里,现在改为独立的一个驱动文件了(gekodriver),但是,它只能驱动Firefox47版本以上(目前最新版本是48.0.2)。
      经过多年的发展WebDriver已经成为了事实上的标准,现在每种浏览器都有独立的官方驱动文件了。如下表:
    Browser
    Component
    Chrome
    Internet Explorer
    Edge
    Firefox 47+
    PhantomJS
    Opera
    Safari

      You will need to download additional components to work with each of the major browsers. The drivers for Chrome, Firefox, PhantomJS, Opera, and Microsoft's IE and Edge web browsers are all standalone executables that should be placed on your system PATH. The SafariDriverbrowser extension should be installed in your browser before using Selenium; we recommend disabling the extension when using the browser without Selenium or installing the extension in a profile only used for testing.
      然后,把这些驱动下载,并存放到一个目录中,例如:D:/driver/ ,然后,把这这个目录添加到系统环境变量PATH下面。


    Usage
      当Selenium-webdriver 被npm下载完成,将到在当前目录下多出一个../node_modules/目录。然后,在与这个目录同级的目录下创建第一个Selenium测试脚本baidu.js。
      The sample below and others are included in the example directory. You may also find the tests for selenium-webdriver informative.
    1. var webdriver = require('selenium-webdriver'),
    2.     By = webdriver.By,
    3.     until = webdriver.until;

    4. var driver = new webdriver.Builder()
    5.     .forBrowser('chrome')
    6.     .build();

    7. driver.get('https://www.baidu.com');
    8. driver.findElement(By.id('kw')).sendKeys('webdriver');
    9. driver.findElement(By.id('su')).click();
    10. driver.wait(until.titleIs('webdriver_百度搜索'), 1000);
    11. driver.quit();
    复制代码
    执行姿势,打开cmd执行。
    1. >node baidu.js
    复制代码
    chrome mobile emulation
    有时候,需要模拟移动端浏览器测试。例子如下:
    1. var webdriver = require('selenium-webdriver'),
    2.     By = webdriver.By,
    3.     until = webdriver.until,
    4.     chrome = require('selenium-webdriver/chrome');

    5. var driver = new webdriver.Builder()
    6.     .forBrowser('chrome')
    7.     .setChromeOptions(new chrome.Options()
    8.         .setMobileEmulation({deviceName: 'Google Nexus 5'}))
    9.     .build();

    10. driver.get('https://m.baidu.com');
    11. driver.findElement(By.name('word')).sendKeys('webdriver');
    12. driver.findElement(By.name('word')).submit();
    13. driver.wait(until.titleIs('webdriver - 百度'), 2000);
    14. driver.quit();
    复制代码
    Using the Builder API
      The Builder class is your one-stop shop for configuring new WebDriver instances. Rather than clutter your code with branches for the various browsers, the builder lets you set all options in one flow. When you call Builder#build(), all options irrelevant to the selected browser are dropped:
    1. var webdriver = require('selenium-webdriver'),

    2.     chrome = require('selenium-webdriver/chrome'),

    3.     firefox = require('selenium-webdriver/firefox');



    4. var driver = new webdriver.Builder()

    5.     .forBrowser('firefox')

    6.     .setChromeOptions(/* ... */)

    7.     .setFirefoxOptions(/* ... */)

    8.     .build();
    复制代码
    Why would you want to configure options irrelevant to the target browser? The Builder's API defines your defaultconfiguration. You can change the target browser at runtime through the SELENIUM_BROWSER environment variable. For example, the example/google_search.js script is configured to run against Firefox. You can run the example against other browsers just by changing the runtime environment
    1. # cd node_modules/selenium-webdriver

    2. node example/google_search

    3. SELENIUM_BROWSER=chrome node example/google_search

    4. SELENIUM_BROWSER=safari node example/google_search
    复制代码
    The Standalone Selenium Server
      The standalone Selenium Server acts as a proxy between your script and the browser-specific drivers. The server may be used when running locally, but it's not recommend as it introduces an extra hop for each request and will slow things down. The server is required, however, to use a browser on a remote host (most browser drivers, like the IEDriverServer, do not accept remote connections).
      To use the Selenium Server, you will need to install the JDK and download the latest server from Selenium. Once downloaded, run the server with
    1. >java -jar selenium-server-standalone-2.45.0.jar
    复制代码
    You may configure your tests to run against a remote server through the Builder API:
    1. var webdriver = require('selenium-webdriver'),
    2. By = webdriver.By,
    3. until = webdriver.until;

    4. var driver = new webdriver.Builder()
    5. .forBrowser('chrome')
    6. .usingServer('http://localhost:4444/wd/hub')  //注意这里
    7. .build();

    8. driver.get('https://www.baidu.com');
    9. driver.findElement(By.id('kw')).sendKeys('webdriver');
    10. driver.findElement(By.id('su')).click();
    11. driver.wait(until.titleIs('webdriver_百度搜索'), 1000);
    12. driver.quit();
    复制代码
    Or change the Builder's configuration at runtime with the SELENIUM_REMOTE_URL environment variable:
    1. SELENIUM_REMOTE_URL="http://localhost:4444/wd/hub" node script.js
    复制代码
    Documentation
    API documentation is available online from the Selenium project. Additional resources include

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏4
    回复

    使用道具 举报

    该用户从未签到

    2#
    发表于 2017-7-11 11:21:35 | 只看该作者
    看了好多文章都是翻译的官网的这个,但是不知道是我配的不对还是怎么着,就是不行,运行测试总是显示:
    timeout waiting for the WebDriver server at http://127.0.0.1:59693/
    at Error <native>.....
    是我那个步骤不对,这个端口总错吗
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-26 00:49 , Processed in 0.071317 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表