JavaScript (node. js) + Selenium Automation test

Source: Internet
Author: User

Selenium is a browser automation library. Most often used for testing web-applications, Selenium May is used for any task this requires automating interaction with The browser.

Selenium is a browser Automation test library, most of the time we use it to test the Web application,Selenium can do any task of automated testing on the browser.

as we all know, Selenium can support multiple programming languages (java/ruby/python/c#/go/javascipt), this blog will show you how to get through javascipt language Writing Selenium Automated test Scripts. Prior to this, the environment needs to be built up.

There is a problem that has not been understood, Javascipt script is not only open browser to execute it? How do I run Selenium ? Do I have to open a browser to execute javascipt to drive another browser to execute? It was not until I saw a bit of node. js Data yesterday that I suddenly understood.

Therefore, you need to first install Node. js.

Official website:https://nodejs.org/en/

installation

Selenium May is installed via NPM with

Selenium can be installed via npm . (npm is a package management tool that is installed with NodeJS.) )

>NPM Install Selenium-webdriver

  Note: Mozilla ' s geckodriver is only required for Firefox 47+. Everything need for Firefox 38-46 are included with the This package.

SeleniumThe authorities have launched3.0, had to celebrate, years of2.xfinally upgraded to3.0out, of course,3.0The official version is also available. Some of these changes have been brought about. One of the biggest changes is thatFirefoxThe browser driver was originally integrated in theSeleniumin the installation package, a separate driver file is now changed (Gekodriver), however, it can only driveFirefox47version above (currently the latest version is48.0.2).

after years of development Webdriver has become a de facto standard, and now each browser has its own official driver file. As the following table:

Browser

Component

Chrome

Chromedriver (. exe)

Internet Explorer

IEDriverServer.exe

Edge

Microsoftwebdriver.msi

Firefox 47+

Geckodriver (. exe)

Phantomjs

PHANTOMJS (. exe)

Opera

Operadriver (. exe)

Safari

Safaridriver.safariextz

You'll need to download additional, and the major browsers. The drivers for Chrome, Firefox, Phantomjs, Opera, and Microsoft ' s IE and Edge Web browsers is all standalone executables That should is placed on your system PATH. The Safaridriverbrowser extension should is installed in your browser before using Selenium; We recommend disabling the extension when using the browser without Selenium or installing the extension in a profiles only Used for testing.

then, download the drivers and store them in a directory, for example: d:/driver/ , then, add this directory to the system environment variable PATH below.

Usage

When Selenium-webdriver is downloaded by npm , it will be one more in the current directory . /node_modules/ directory. Then, create the first Selenium test script baidu.jsin the directory sibling to this directory.

The sample below and others is included in the example directory. Also find the tests for selenium-webdriver informative.

var webdriver = require (' Selenium-webdriver '),    = webdriver. By,    = webdriver.until; var New Webdriver. Builder ()    . Forbrowser (' Chrome ')    . Build ();d river.get (' https://www.baidu.com '); Driver.findelement (by.id (' kw ')). SendKeys (' Webdriver ');d river.findelement (by.id (' su ')). Click ();d river.wait (Until.titleis (' webdriver_ Baidu Search '),;d river.quit ();

Execute posture, open cmd execution.

>node Baidu.js

Chrome Mobile Emulation

Sometimes, you need to simulate a mobile browser test. Examples are as follows:

varWebdriver = require (' Selenium-webdriver ')), by=Webdriver. By, until=Webdriver.until, Chrome= Require (' Selenium-webdriver/chrome ');varDriver =NewWebdriver. Builder (). Forbrowser (' Chrome '). Setchromeoptions (NewChrome. Options (). Setmobileemulation ({devicename:' Google Nexus 5 '}). Build ();d River.get (' Https://m.baidu.com ');d river.findelement (By.name (' word '). SendKeys (' Webdriver '));d river.findelement (By.name (' word ') . Submit ();d river.wait (Until.titleis (' Webdriver-Baidu '), 2000);d river.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 your set all options in one flow. When you Call builder#build (), all options irrelevant to the selected browser is dropped:

 var  webdriver = require (' Selenium-webdriver '  = require (' Selenium-webdriver/chrome '  = require (' Selenium-webdriver/firefox ' );  var  driver = new   Webdriver. Builder (). Forbrowser ( ' Firefox ' ). Setchromeoptions ( /*   ...  */  /*   ...  */  

Why would your 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 was configured to run against Firefox. You can run the example against other browsers just by changing the runtime environment

# CD Node_modules/selenium-webdrivernode example/google_searchselenium_browser=chrome node Example/google_searchselenium_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 is used when running locally, but it's not recommend as it introduces a extra hop for each request and Wil L Slow things down. The server is required, however, to use a browser in a remote host (most browser drivers, like the Iedriverserver, does not Accept remote connections).

To use the Selenium server, you'll 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

Configure your tests to run against a remote server through the Builder API:

 var  webdriver = require (' Selenium-webdriver '  = Webdriver. By,until  = Webdriver.until;  var  driver = new   Webdriver. Builder (). Forbrowser ( ' Chrome ' ). Usingserver ( ' http://localhost:4444/ Wd/hub ') //  note here  .build ();d river.get ( ' https://www.baidu.com '  );d river.findelement (by.id ( ' kw ')). SendKeys (' webdriver ' ); Driver.findelement (by.id ( ' su '  ' Webdriver_ Baidu Search '), 1000);d river.quit ();  

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

Selenium_remote_url= "Http://localhost:4444/wd/hub" node script.js

Documentation

API documentation is available online from the Selenium project. Additional Resources include

    • The #selenium channel on Freenode IRC
    • The [email protected] list
    • SELENIUMHQ Documentation

JavaScript (node. js) + Selenium Automation test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.