Headless Browser in Selenium using PhantomJS-Pros and Cons



PhantomJS is a headless web browser scriptable with JavaScript which is similar to HTMLUnitDriver. Click on this link to view the examples of the HTMLUnitDriver.

PhantomJS runs on Windows, macOS, Linux, and FreeBSD. Currently the PhantomJS development is stopped due to less support from the group.

Pros of Headless Browser:

  • Headless browser executes the scripts faster then the real browser.
  • Helps us in improves the performance execution of the scripts.
    Headless browser saves time.

Cons of Headless Browser:

  • Headless browser does not mimic the real execution of the test-cases.
  • It not possible to view the steps visually during the execution of the test-cases.

Like our Facebook Page to get frequent updates->

https://www.facebook.com/totalqa


Selenium WebDriver Code

1. Add the following dependency in the pom.xml to add the PhantomJS Library to the build path:


2. Instead of the setting the path to the Phantom JS executable. Use the WebDriverManager class ensure the following depedency is added in the maven project object model (pom.xml) file.


3. Execute the below code in eclipse and verify the results.

Conclusion

Selenium Code executed successfully without launching the Browser.

HeadLess Browser using the PhantomJS Driver

HeadLess Browser using the PhantomJS Driver

using WebDriverManager class in Selenium WebDriver



Selenium WebDriver automation tool is mainly useful to run tests against multiple Browsers such as Chrome Browser,Firefox Browser,Internet Explorer Driver.Running the tests against multiple browsers is known as Cross Browser Testing.
To launch the Browser in Selenium WebDriver we need to set the path of the executable available in the laptop. Following that we have to instantiate the driver instance based on the Driver instance. In order to avoid these steps we can use the WebDriverManager class.

WebDriverManager

WebDriverManager allows to automate the management of the binary drivers (e.g. chromedriver, geckodriver, etc.) required by Selenium WebDriver.
Using WebDriverManager its not required to maintain the below lines of the code for launching the browser.
System.setProperty(“webdriver.chrome.driver”, “/path/to/binary/chromedriver”);
System.setProperty(“webdriver.gecko.driver”, “/path/to/binary/geckodriver”);
System.setProperty(“webdriver.opera.driver”, “/path/to/binary/operadriver”);
System.setProperty(“phantomjs.binary.path”, “/path/to/binary/phantomjs”);
System.setProperty(“webdriver.edge.driver”, “C:/path/to/binary/MicrosoftWebDriver.exe”);
System.setProperty(“webdriver.ie.driver”, “C:/path/to/binary/IEDriverServer.exe”);

WebDriverManager Maven Depdendency

To use the WebDriverManager class ensure the following depedency is added in the maven project object model (pom.xml) file.


Like our Facebook Page to get frequent updates->

https://www.facebook.com/totalqa


Selenium WebDriver Code

Execute the below code in eclipse and verify the results.

Steps to Launch the ChromeBrowser without using chromedriver.exe



Selenium WebDriver automation tool is mainly useful to run tests against multiple Browsers such as Chrome Browser,Firefox Browser,Internet Explorer Driver.Running the tests against multiple browsers is known as Cross Browser Testing.
To launch the Browser in Selenium WebDriver we need to set the path of the executable available in the laptop. Following that we have to instantiate the driver instance based on the Driver instance. In order to avoid these steps we can use the WebDriverManager class.

WebDriverManager

WebDriverManager allows to automate the management of the binary drivers (e.g. chromedriver, geckodriver, etc.) required by Selenium WebDriver.
Using WebDriverManager its not required to maintain the below lines of the code for launching the browser.
System.setProperty(“webdriver.chrome.driver”, “/path/to/binary/chromedriver”);
System.setProperty(“webdriver.gecko.driver”, “/path/to/binary/geckodriver”);
System.setProperty(“webdriver.opera.driver”, “/path/to/binary/operadriver”);
System.setProperty(“phantomjs.binary.path”, “/path/to/binary/phantomjs”);
System.setProperty(“webdriver.edge.driver”, “C:/path/to/binary/MicrosoftWebDriver.exe”);
System.setProperty(“webdriver.ie.driver”, “C:/path/to/binary/IEDriverServer.exe”);

WebDriverManager Maven Depdendency

To use the WebDriverManager class ensure the following depedency is added in the maven project object model (pom.xml) file.


Like our Facebook Page to get frequent updates->

https://www.facebook.com/totalqa


Selenium WebDriver Code

Execute the below code in eclipse and verify the results.

Capturing screenshot using java.awt.Robot class-Selenium WebDriver



The java.awt.Robot class is mainly useful to perform native actions where keyword and mouse actions to be performed. This actions are helpful for automation. If the current platform configuration does not allow input controls, an AWTException will be thrown when using the java.awt.Robot class. In this post we will java.util.Robot class for capturing the screenshots. The method createScreenCapture(Rectangle screenRect) is available in java.awt.Robot class which helps to create an image containing pixels read from the screen.The java.awt.Rectangle object to be passed as an parameter for capturing the screenshot. To create an Rectangle object the java.awt.Dimension[Height*Width] object to be passed as an parameter.

API Classes and Methods useful to capture the Screenshot as follows:

  • Robot Class Object
  • Rectangle Class Object
  • Dimension Class Object- Input Parameters Height * Width

Selenium WebDriver Code

Output:

The full screen is captured as screenshot which is the expected outcome as shown below:

Capture Screenshot using java.awt.Robot Class

Capture Screenshot using java.awt.Robot Class

Regex Pattern Matching in JAVA – String Functions startsWith(),endsWith(),contains()



To identify particular pattern of regular expressions in Strings is achieved in JAVA by using the API java.util.regex. This API consists of two main java classes such as Matcher and Pattern classes. Also we can even use String Functions to identify the particular pattern of regular expressions in the String class.

Pattern Class

A regular expression specified as a String is passed into an instance of this class.
The output is used to create a Matcher object that can match the character sequences against the regular
expression.

Static methods available in the Pattern Class as follows:

  • pattern()
  • split()
  • matcher()
  • compile()
  • matches()

Matcher Class

This java class helps to matches the character sequences by interpreting the pattern.

Static Matcher Methods in the API:

  • start()
  • end()
  • group()

Identify the List of Strings Starts with a particular letter

Output of the Program:

Result:: false
Result:: false
Result:: false
Result:: true
Start index: 0 End index: 1 Group value::t


String Functions to identify the Regular Expressions:

  • startsWith(String prefix)
  • endsWith(String suffix)
  • contains(CharSequence s)

Identify the List of Strings Starts with a particular letter using String Functions

Output of the Program:

String starts with a particular character:::total-qa
String starts with a particular character:::tutorialpoint
String ends with a particular character:::guru99
String contains with a particular character:::total-qa
String contains with a particular character:::javatpoint
String contains with a particular character:::tutorialpoint


Conclusion:

Using the String functions and java.util.regex API, identification of the pattern in the given Strings is possible using the above examples.

Add,Remove,Update – ArrayList,LinkedList,Vector Mock Interview Questions



List is a type of Collection mainly useful to store the Objects which are duplicates as well. Its available in the package java.util.List. The behavior of the List is implemented by the java classes

  • ArrayList
  • LinkedList
  • Vector

Java Mock Interview Questions about java.util.List as follows:

1. Adding an new item to the List
2. Adding an new item first to the List.
3. Adding an item from in the middle of the List.

4. Remove an item from the List.
5. Remove an item from the List at particular index.
6. Update an item from the List.

Conclusion:

Addition,Deletion and Updating the items of List is achieved using the methods available in the java.util.List package.

Dynamic Input Test Data Generation: Best Practices,Random Java API



In Testing it is important to provide input data to complete the execution of Test-cases. Irrespective of any type of testing such as Functional Testing,Integration Testing,System Testing and Performance Testing the input data plays a vital role.
In Automation tools such as Selenium WebDriver the input data can be maintained in three different ways:

1. Maintain parameters in the testng.xml. Read the parameter values using @Parameters annotation.
2. Maintain input data in the Excel File. Read the data and pass the values using @DataProvider annotation.
3. Input Data in the properties file.

In the above 3 cases, the input data stored is static. If we suppose, the dynamic data should be generated used in the execution flow of test cases. We have to Java API to generate the dynamic data.
So, we will go through now how to generate the random/dynamic input data.

Note:
Do not get confuse with the dynamic object properties. In this post we are discussing about only the input data.

Generating two digit Random Number

java.util.Random is a java class available for generating random integers.

Generating three digit Random Number

Generating Random ASCII Character

To generate the random characters we should glance through the ASCII table from this link.
http://www.asciitable.com/

      Lower Case Alphabets– Decimal Number Ranging from 97 to 122
      Upper Case Alphabets– Decimal Number Ranging from 65 to 90

Generating five digit Random Number

Conclusion:

The Dynamic input data generated is useful to execute the test case in different Testing Environment without any dependency on resource files.

Selenium IDE Recording and Playback – Get this Extension for Chrome



Selenium IDE is one of the important Component of Selenium. IDE stands for Integrated Development Environment useful for Record and Playback and helps for getting acquainted with Selenium Web-Driver.
The new version Selenium IDE is designed to record and playback interactions with different browsers such as ‘Mozilla Firefox’ and ‘Chrome‘.

Steps for downloading IDE on Firefox Browser(Pre-requisite – Firefox)

1. Download the Mozilla Firefox by accessing the link ->https://www.mozilla.org/en-US/firefox/new/

2. Access Firefox and open the URL http://seleniumhq.org.

3. Navigate to Download tab look for Selenium IDE.

4. Download latest released version of Selenium IDE for Firefox by clicking on the Link Firefox which redirects to the url ->
https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/

Steps for downloading IDE on Chrome Browser(Pre-requisite – Chrome)

1. Download the Chrome Browser by accessing the link ->https://www.google.com/chrome/

2. Access Firefox and open the URL http://seleniumhq.org.

3. Navigate to Download tab look for Selenium IDE section.

4. Download latest released version of Selenium IDE for Firefox by clicking on the Link Firefox which redirects to the url ->
https://chrome.google.com/webstore/detail/selenium-ide/mooikfkahbdckldjjndioackbalphokd

Record and Playback the Scripts on Chrome Browser:

1. Open Selenium IDE.

Selenium IDE - Chrome Browser

Selenium IDE – Chrome Browser


2. Click on the link Record a new test in a new project.
Selenium IDE - Record a new test in a new project

Selenium IDE – Record a new test in a new project

3. Provide the project name and click on OK button.

Selenium IDE Project Name

Selenium IDE Project Name

4. Provide the Base URL and click on START RECORDING button.

Base URL -> http://www.seleniumhq.org

Base URL -> http://www.seleniumhq.org

5. A new Chrome browser opens with the Base URL. Perform the steps and check the recorded steps in the background.
Click on Stop Recording button in Selenium IDE to complete Recording.

Selenium IDE - Recording and playback on  Chrome Browser

Selenium IDE – Recording and playback on Chrome Browser

Conclusion:

Selenium IDE add-on helps us to record the steps on Chrome Browser as well.
It also helps us to identify the properties of the Web-Elements available in the Web Application.

How to use Selenium Grid easily in 3 Steps?

      No Comments on How to use Selenium Grid easily in 3 Steps?


Selenium-Grid allows you run your tests on remote desktop against different Operating Systems like Windows,Linux,MAC and against different browsers like Opera,Safari,IE,Firefox and Chrome in parallel. Selenium-Grid allows running your tests in a distributed test execution environment.


Grid Architecture:

In Selenium-Grid, the Component Hub acts as a server receives the requests from Selenium WebDriver program and redirects requests to the Nodes which are configured to Hub. Refer to the details in the diagram mentioned below:

Grid Architecture

Grid Architecture


Installation:

Ensure the Selenium Standalone Server Jar is downloaded in Hub and Node Systems as well.
Download the Selenium Standalone Server Jar from this link -> http://selenium-release.storage.googleapis.com/index.html


Starting Hub:

The Hub is the central point that will receive all the test request and distribute them the the right nodes.
Open the new command line and execute the below command:
java -jar selenium-server-standalone-xxx.jar -role hub -port 4545


Starting Node 1:

Use the same http://selenium-release.storage.googleapis.com/index.html jar file to start the nodes and connect to the Hub.
Capabilities for Node 1:

  • Platform : WINDOWS
  • Browser Name: firefox
  • Browser Version:123

Open the new command line and execute the below command:


java -Dwebdriver.gecko.driver=”C:\\XXXX\\geckodriver-v0.23.0-win64\\geckodriver.exe”
-jar selenium-server-standalone-3.141.59.jar
-role webdriver
-browser “browserName=firefox,version=123,maxInstances=1,platform=WINDOWS”
-hubHost localhost
-hubPort 4444
-port 4546


Starting Node 2:

Use the same http://selenium-release.storage.googleapis.com/index.html jar file to start the nodes and connect to the Hub.
Capabilities for Node 2:

  • Platform : WINDOWS
  • Browser Name: chrome
  • Browser Version:40

Open the new command line and execute the below command:


java -Dwebdriver.chrome.driver=”C:\\XXXX\\chromedriver_win32\\chromedriver.exe”
-jar selenium-server-standalone-3.141.59.jar
-role webdriver
-browser “browserName=chrome,version=40,maxInstances=1,platform=WINDOWS”
-hubHost localhost
-port 4547


Selenium WebDriver Testng Program:


Results:


To check the ports which are reserved

To verify that the port number 4444 is in use. Execute the below command in the command line:
netstat -a -n | findStr 4444

Conclusion:

Selenium Grid is useful to run test cases remotely with specific capabilities.

[CSS Tips]white-space-hyphen-minus selenium

      No Comments on [CSS Tips]white-space-hyphen-minus selenium


In this tutorial we will discuss about the identifying the Selenium Locator for the Css (Cascading Style Sheets) in the following scenario’s

  • white spaces in an Web Element attribute value
  • hyphen-minus (-) in an Web Element attribute value
  • Attribute Selector
  • Type Selector


HTML Source Code for a Text-box having spaces for attribute id value:

Represents an element with the attribute whose value is a white space-separated list of words, one of which is exactly “val”.
id="pass word".

Syntax:

[att~=val]

Examples:

[id~=pass]
[id~=word]

Selenium Locators-Css white space

Selenium Locators-Css white space


HTML Source Code for a Text-box having hyphen-minus for attribute id value:

id="pass-word".

Syntax:

Represents an element with the attribute, its value either being exactly “val” or beginning with “val” immediately followed by “-”
[att|=val]

Examples:

[id|=pass]

Css attribute value having hyphen-minus special character

Css attribute value having hyphen-minus special character


Attribute Selector:

Match when the element sets the “att” attribute, whatever the value of the attribute.

Example:

[id] – Selects all the Elements having ‘id’ attribute.
[type]- Selects all the Elements having ‘type’ attribute.

Css Locator - identify all the elements having <id> as attribute

Css Locator – identify all the elements having type as attribute

Type Selector:

Matches every instance of element Type.

Example:

h1 – Selects all the elements having h1 as tag-name.
input- Selects all the elements having input as tag-name.

Type Selector in CSS

Type Selector in CSS

Conclusion:

In this topic we have discussed about Handling CSS attribute values from Selenium Web-driver perspective which are having white spaces and special characters hyphen-minus (-).