Protractor setup and example of non angular app simple test – Part 1

Now a days we see lot of angular JS applications being built.

It is said that at least 10% of the latest developing are based on angular js.

In this section we see how to configure and run a simple protractor test on angular and non angular app

Its very straight forward, mostly thats the reason why its largely adapted.

Step 1 :

Install Node.js, current version of node js when writing this blog is v10.16.2

you can download node js from  Node Js download

select the windows MSI and install it. While installing it select add node js to path.

This option enable us to directly access node commands from cmd without navigating to node directory.

Node JS is like collection of several java script libraries. They are called as node modules

The installation also installs the node package manager which is required to install different node modules.

Once you have installed it  go to command prompt (search-> run – >cmd)

then enter following command

Node -v

It should show the current installed version, if its not there is some issue with the installation.

Step2:

Now we have node js setup and ready

Open cmd and try to install the protractor, install protractor globally (all users on the machine, if not remove -g)

npm install protractor -g

More information on this site https://www.protractortest.org/#/

Usually protractor sets up the web driver manager. But if it’s not then you need to install the webdriver manager as well

webdriver-manager update

Step3:

Now we have to start our webdriver-manager to kick of the example test

Webdriver manager will look for webdriver connections that are being initiated by protractor.

webdriver-manager start

There is one sample test that comes with protractor we can use it and create couple of more tests.

But, if we want to run on google we might want to make some changes.

At the end of start of webdriver you should see command prompt like below.

Capture

Step4:

Once you started the webdriver manager, open another command prompt and run a angular example test

In the node js installation will happen typically in appdata/roaming folder go to following path, you should see following

Example path :

C:\Users\{user_name}\AppData\Roaming\npm\node_modules\protractor\example

You should see the following, googleTest is something I have added, you don’t see it by default.

scriptlocation

To run a test we need to use following command

Protractor C:\Users\{user_name}\AppData\Roaming\npm\node_modules\protractor\example\conf.js

What does this mean ?

Protractor reads the conf file and executes the specification file mentioned in the conf.js

Protractor uses something called Jasmin framework by default. Its called as “Describe It” framework.

Test start with Describe() – > then It() , which means action and its expected outcome.

All the code is written in java script, we can use type script too.

Once we start the example test it will perform operations on node js site and gives us the following result.

The result displayed below is formatted, I will mention how to configure different types of reports in next section.

Result

In the next part will discuss about following

  • Custom report as shown above.
  • conf.js sections and how to edit.
  • googleTest.js -> non angular application example.

 

Synchronization in Automation Testing with selenium.

When you ask selenium tester what is the most common issues he is facing during his automation testing experience, the first thing he would say is Sync problem.

Let’s take a bird eye view on this and will take deep dive on the synchronization issues and how to address them.

Selenium framework has several methods to address the synchronization issues. Mainly they are segregated into two different varieties.

  • Implicit waits
  • Explicit waits

Implicit wait:

This is the most common wait type we use; it is also called as default wait. The page load time out, and the timeouts.implicitwait is the two types of implicit wait. The implicit wait meaning that the driver has to wait until the web element is existed on the dom. It will come out of the waiting loop if element exists beforehand. Example you have given 10 seconds on element x, and it has loaded in 2 seconds web driver knows it and goes to next step without waiting for 10 seconds. It is called as implicit wait.

driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Explicit wait:

Explicit waits are defined on the particular web element to wait until the property value becomes true.

It will pole every 500 milliseconds to get the property value and verifies it with defined value.

Examples:

//webdriver wait class
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath(menu))));

//for multiple elements
WebDriverWait wait = new WebDriverWait(driver, 30);
List options = driver.findElements(By.xpath(option_to_select));
wait.until(ExpectedConditions.visibilityOfAllElements(options));

//Another example
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.invisibilityOfElementWithText(By.className("jqx-fill-state-normal"), "Loading..."));

Fluent wait:

Fluent waits also called as explicit waits, but only difference being we can define interval to pole and can tell to ignore any exceptions. It will be useful to save resources not to check every 500 milliseconds if element load takes long time.

Wait<WebDriver> fluentwait = new FluentWait<WebDriver>(driver)
                        .withTimeout(20, TimeUnit.SECONDS)
                        .pollingEvery(1, TimeUnit.SECONDS)
                        .ignoring(java.util.NoSuchElementException.class);
fluentwait.until(ExpectedConditions.invisibilityOfElementWithText(By.className("jqx-fill-state-normal"), "Loading..."));

The another type of wait is normally not recommended is static wait or pause the program.

Thread.sleep(500);

So, the gist is the know when to use what. Fluent wait is used when application slower and running in a slow paced machine where we don’t want driver to hit every 500 milliseconds, and you know it will not come till 2 sec or 3 seconds, we can define the polling interval. I am guessing this as it is synonymous to implicit wait where the default polling is 500 ms.

We can use web driver wait almost in all cases where we know element loading is taking place, but the properties are not loaded until some other action is completed, example, state dropdown is enabled only when country drop down is selected, in that case we can use expected conditions on the enabled of the state dropdown after country select.

Default waits or implicit waits are used when we know dom loading is taking some time and not to give wait on existence of each element. Normally this would serve the purpose, but angular applications waits plays key role in test execution failures.

If we use waits wisely and where ever we need depending the application behavior with limited use of static waits will improve the script quality drastically.

Thank you.

Selenium – Overview

Introduction

Hello,

Recently, I met one of my colleague  who is preparing for Automation Test interviews.

As I do take interviews in my organization, I thought why not I throw my two cents on this topic. As the thought goes into action you see me here. I hope it helps people who are preparing for interviews and automation learners.

Every interviewer’s perception and thought process for selecting a candidate will be different. In my view one should know below three areas of the automation testing for that matter in any technology or the software we are learning or planned to learn.

  • Conceptual details :
    • This would cover the concepts of the tool/technology what we are planning to use where does this tool exactly fit in place.
  • Technical details:
    • This will give us complete information of the tool/technology implementation details.
  • Managerial details
    • This is the last area but the most important area, this will give us picture of what are the efforts and risks involving this tool and this implementation.
    • At the end this is the most deciding factor for choosing the technology and tool that we are planned to use.

In this blog I tried to put focus on above three mentioned areas and come up with general questions that an interviewer might ask.

and the details

Conceptual

What are the factors to choose the Selenium vs other tools ? why not UFT or other tools ?

  • Selenium is free library and driver owners are constantly updating the driver exes
  • Selenium is best suited for multiple browser testing with Java/.Net/python etc, if we have skilled resources available we can always utilize them for building the automated tests
  • Selenium can be integrated easily with BDD with Cucumber for business driven development for modern development environment
  • It can be easily integrated with CICD process with Jenkins or with TFS
  • It is not suited for the applications involving interactions with windows UI objects.
  • Auto IT is the free tool combined with Selenium is widely used for limited windows operations and more html DOM operations.
  • UFT tool is very much suited for HP tool set environment like ALM. It is also most widely used tool in the automated testing space, as it is licensed tool and license is costlier Its slowly loosing the market share. It only supports vb where as Java resources much widely found. Distributed testing can be easily done with this combination as test management tool like ALM, it has capabilities to run scripts on multiple host machines.
  • Selenium Grid can be used for distributed testing, if Jenkins is used, we can configure multiple Slave nodes containing different browsers and operating systems.
  • If regression suite size is large it would be good to use frame works like testNG or cucumber for better maintainability.

 What is Jenkins and CICD process how does Selenium fit in this ?

  • CICD is continuous integration and continuous development
  • It is the integrated part of modern fast paced development methodologies like Agile Scrum.
  • CICD , CI part of it is being when build gets generated there will be set of automated tests will run to verify the build stability.
  • Jenkins is the tool which is widely used to build the code in distributed (there are lot of other tools which supports CICD environment) we can add build steps in this tool
    • Post build operation being triggering the automated build
    • Jenkins will receive feedback from the automated tests and it will mark build pass/fail/warning based on the test results.
    • CD part being development of new code/ enhancements based on the failures or new features.

Technical:

What is selenium ?

  • Selenium is code library that has methods developed to interact with modern browsers.
  • The actions on the browsers are performed by driver exes. Selenium methods can talk to browsers through these driver exes.
  • We can send or receive information from driver exes through the selenium methods.

What is xpath ?

  • Xpath is the general location of an web element in the HTLM dom (document object model)
  • This will tell us the location or generalized location of element or elements in the DOM tree
  • This is most widely used and modern browsers supports the xpath search.
  • This is fast and not mostly reliable as the dom structure will dynamically change in the modern web technologies.

What is css selectors ?

  • Css selector is the why selenium identifies the object similar to Xpath
  • Selenium can read the css files and it can identify the objects based on css classes.
  • Css selectors and xpaths are most commonly used methods to identify the object

In web application driver.findement(by.xpath) not recognizing te objects. Application is html based we can see element in the dom, what might be the issue ?

  • The most common reason for this is being that element is in iFrame.
  • We need to switch the driver to that iFrame
    • Before that we need to find all elemnts with iFrame tag get either ids or names of the frames and switch to that frame
    • With Driver.switchto
    • Then if we use find elements we can find the element.

What is TestNG xml ?

  • TestNG xml is the source or driving the test suites build on TestNG framework
  • We can give test classes and test groups information to run the tests.

How can we identify a link in the application ?

  • Link can be identified with by.linktext or by.partiallinktext

What are the different ways to identify elements ?

  • By.TagName
  • BY.Xpath
  • By.cssseector
  • By.classname

How do handle elements with in the elements say for example data in table ?

  • We can iterate the findelements with in the elements like below
  • First find Elements with tag name tr
List <WebElement>rows = Driver.FindElements(By.tagnme(“tr”))
for (WebElement row :rows)
 {
   List<WebElement> dataincolumns = Row.findelements(By.tagname(“td”))
 }
  • This is just illustration that we can  do following
Driver.FindElement(By.tagname(<locator>)).FindElement(By.xpath(<locator>).FindElement….. etc

How can we get the text in the textbox

  • We use element.getAttribute to get the attribute of elements such as value and color
  • Element.getAttribute(value) will give us the text of the element.

What are actions ?

  • Actions is class for mouse interactions
  • Some of the web elements need to be hovered to get the list in the common web page designs.
  • We need actions class to perform those operations.
  • Examples:
    • Hovering
    • Mouse move

what is test framework ?

  • test framework is well defined process for creating, execution reporting the automated tests.
  • We see lot of customized framworks in each organizations and some free framework tools also available
    • Junit
    • TestNG
    • Cucumber
  • The main concepts for designing framework should be
    • Process to easily create tests
    • Process to pass data to tests
    • Process to easily maintain the tests
    • Process to generate required reports

Managerial

How can we report with selenium

  • Reporting is bit of crude in selenium, if we use standard frameworks we can get html report
  • Generally we create expected result and actual result in array lists and at the end of the tests we flush them to excel.

How can we manage the code repository if many people are working

  • Selenium development of tests is same as any development project.
  • We use SVN or TFS code management tools to make regular check ins and check outs.
  • SVN is free , and most widely used code management tool, for Microsoft technologies we use TFS as code management.

What is Maven ?

  • Maven is the repository for Jar files. Jar files are nothing but java libraries which has functions or methods readily available for usage.
  • When building a project with selenium we require to use standard java libraries for different purposes
  • Examples:
    • testNG jar file
    • Apache poi jar file
  • If multiple people are using the project, sharing of these libraries will become challenge.
  • Maven project will remove this dependency by adding all dependent jar files directly from Maven repository.
  • Maven will create local repository and down loads all required jar files in the local system. We need to make sure we add all dependencies in the pom.xml.

These are some of the commonly asked High level overview questions in the Selenium interviews. I see we did not cover everything in very detail as it is just overview.

I will try to cover each area with important aspects in my next blogs. Thank you.