Monday, January 21, 2019

Selenium + JavaScript Best Practices


We all knows that by including unit tests in your web application projects you may lead to several advantages. Mainly, an impressive and measurable method of proving code quality to developers and other project stakeholders. Browser automation takes these concepts even further, and applies the practice to running integration and regression tests live within the presentation layer user interface (UI). From there on, we can codify integration test scripts that do all the work for us – proving that the web app works as expected in the platform in which it will ultimately run.

Today’s technology world, especially in automation testing sector,one of the most known and implemented tools used for web browser automation is Selenium. It is not only intended to be used in automating web applications for testing intent, but also for automation of web-based administration work. In fact, it is a set of different software tools, each with a different approach to supporting test automation. Learning all of its tools will give you many different options for approaching different test automation problems.

JavaScript frameworks

Many software programs are composed as web-based applications that operate in an Internet browser. This is the most important reason that net frameworks are becoming increasingly more popular among other frameworks. Now, JavaScript is a widely used language for web software development, and even back-end developers favor JS rather than every other language. It's easy and flexible, but in exactly the identical time it also allows building complex web options. The advantages of using JavaScript frameworks are as follows:

Performance -- Projects that used to take weeks and hundreds of lines of code, can currently be achieved much quicker with well-structured pre-built patterns and functions.

Security -- Top JavaScript frameworks have company security agreements and are encouraged by large communities where users and members also act as testers.

Price -- Many frameworks are open source and totally free. Since they assist developers to build custom solutions faster, the ultimate price for internet apps will probably be reduced.

Combining Selenium and JavaScript

One of the most popular JavaScript applications menus for creating dynamic web sites and internet applications is MEAN stack (MEAN stands for Mongo DB, Express, Angular and Node.js). Here, I'll utilize Selenium integration with Node.js, because it's simple, nicely organized and quite suitable for use for test automation functions.

Node.js framework and Selenium installation

The code example below generates a very simple validation test for the google web page. To begin writing Selenium tests with Node.js and coffee script, we all need to get a Node.js frame using a Selenium plugin added to it.

If you installed Node.jsthen start a command line and then type these commands to make a new Node.js Project and init the fundamental job structure:

mkdir google_test

cd google_test

npm init

NOTE: in case you haven't previously installed Node.js, download it from its official site .

Next step will be to set up the node browser automation library called"selenium-webdriver". In the console you started previously, use Node's built-in package supervisor (NPM) and type the following command to get the package:

Npm install --rescue selenium-webdriver

The option"--rescue " creates a new bundle that will be saved into the project's package.json file.

Before we can begin to control the browser we will need Selenium WebDriver to your particular browser that we need to automate (IE/FireFox/Chrome/Edage). Here, I'll show an example with gecko driver for Firefox. You can obtain a driver to get a desired operating system from their github project:




When you get into the driver, then unzip it and put in on your own operating system on the default location. You'll Also have to upgrade the environment PATH variable:



 Writing tests using Selenium

Manual site regression testing can take quite some time, as it needs a tester to run each test one at a time across various browsers. Running tests asynchronously across different browsers will conserve a great deal of time, and Selenium WebDriver provides this functionality. Selenium handles asynchronous analyzing by using JavaScript promises.

Now, let us write the first code and make one simple test that assesses if Selenium functions stand alone, without using Mocha. The test will do the next steps to ensure that we actually reach the google webpage:

Open google web page.
Get its title by using promise.
Output it to the console.

NOTE: You can use any editor of your choice, however I advise using Atom that works well with the Node.js frame. You can also set up the atom-ternjs plugin to it, which may add support for Node.js, Angular along with other languages.

Create a new JavaScript file on your job, multiplying: first_test. js.
Add the following code to it



Here's a Brief code explanation:

From the first 2 lines we place example of selenium-webdriver, also assembled our browser using the webdriver and its Firefox plugin.

Then we browse into the google web page and receive its name by applying JavaScript's guarantee
We direct the title to the console and quit our browser.

Let us do this evaluation by using the following Node.js control:

node first_test. js

We'll get the following output:



This means we are on the ideal track, and also Selenium driver is working.