JavaScript+Selenium自动化测试_selenium和js能一起做自动化测试

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.

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;
 
var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build();
 
driver.get('https://www.baidu.com');
driver.findElement(By.id('kw')).sendKeys('webdriver');
driver.findElement(By.id('su')).click();
driver.wait(until.titleIs('webdriver_百度搜索'), 1000);
driver.quit();

执行姿势,打开cmd执行。

>node baidu.js

chrome mobile emulation

有时候,需要模拟移动端浏览器测试。例子如下:

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until,
    chrome = require('selenium-webdriver/chrome');
 
var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .setChromeOptions(new chrome.Options()
        .setMobileEmulation({deviceName: 'Google Nexus 5'}))
    .build();
 
driver.get('https://m.baidu.com');
driver.findElement(By.name('word')).sendKeys('webdriver');
driver.findElement(By.name('word')).submit();
driver.wait(until.titleIs('webdriver - 百度'), 2000);
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:

var webdriver = require('selenium-webdriver'),
 
    chrome = require('selenium-webdriver/chrome'),
 
    firefox = require('selenium-webdriver/firefox');
 
 
 
var driver = new webdriver.Builder()
 
    .forBrowser('firefox')
 
    .setChromeOptions(/* ... */)
 
    .setFirefoxOptions(/* ... */)
 
    .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

# cd node_modules/selenium-webdriver
 
node example/google_search
 
SELENIUM_BROWSER=chrome node example/google_search
 
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

java -jar selenium-server-standalone-2.45.0.jar
You may configure your tests to run against a remote server through the Builder API:

var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
 
var driver = new webdriver.Builder()
.forBrowser('chrome')
.usingServer('http://localhost:4444/wd/hub')  //注意这里
.build();
 
driver.get('https://www.baidu.com');
driver.findElement(By.id('kw')).sendKeys('webdriver');
driver.findElement(By.id('su')).click();
driver.wait(until.titleIs('webdriver_百度搜索'), 1000);
driver.quit();

Or change the Builder’s configuration at runtime with the SELENIUM_REMOTE_URL environment variable:

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值