How to encode the password for selenium using java


when you automate any application we do not want to share the password to outside the world. So, we will use Base64 class of the Java to encode and decode strings or passwords to use securely. In this article I will show you a practical selenium program to achieve the same thing how to encode the password and use the same to log into the application.


Headless Browser Testing in Selenium Web Driver


A headless browser is a browser simulation program that does not have a user interface (UI less).
Headless browser programs operate like any other browser, but do not display any UI. Selenium executes its' tests in the background.

There are several headless browsers available in the market, the following are the most popular ones:
  • Chrome
  • Firefox
  • HTMLUnit driver
  • PhantomJS

How to Check Drop down options are sorted or not in selenium with Java


In this post, I'm going to explain how to check Drop down options are sorted or not in selenium webdriver with java.
  • Open the browser and navigate to the webpage
  • Find the dropdown using the findElement method in selenium
  • Create a object to Select class and pass the dropdown element as the parameter to constructor
WebElement element = driver.findElement(By.xpath("//*[@id=\"animals\"]"));
Select se = new Select(element);
  • Using getOptions() method from Select class you can get all the options from the dropdown in the form of WebElement.
  • Using the loop we can retrive the values from the List of WebElement
  • Add all the values into a list called originalList that we have already created
ArrayList originalList = new ArrayList();

for (WebElement e : se.getOptions()) {
originalList.add(e.getText());
}
  • The values we retrieved could be sorted or not sorted values [ we are not sure, we have to verify this]
  • Now lets create a temporary list alled tempList and get the values from originalList
  • Now sort the Either tempList or originalList and compare them, We can sort the list using the Collections.sort(list) method
ArrayList tempList = originalList;
Collections.sort(tempList); 
  • We can compare the list using conditional statement
Complete program for not working sorting

import java.util.ArrayList;
import java.util.Collections;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class VerifyDropDownSortedOptions {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("https://testautomationpractice.blogspot.com/");

WebElement element = driver.findElement(By.xpath("//*[@id=\"animals\"]"));
Select se = new Select(element);

ArrayList originalList = new ArrayList();

for (WebElement e : se.getOptions()) {
originalList.add(e.getText());
}
System.out.println("originalList:" + originalList);

ArrayList tempList = originalList;
Collections.sort(tempList); // When you change one list, it changes the other list as well.

System.out.println("originalList:" + originalList);
System.out.println("tempList:" + tempList);

/*So the test gets pass all the time because the sequence in the originalList
and tempList is going to be same.
If you are following above process then your test never fails, because When
you change one list, it changes the other list as well.*/

if (originalList == tempList) {
System.out.println("Dropdown sorted");
} else {
System.out.println("Dropdown NOT sorted");
}
driver.close();
}
}

So the test gets pass all the time because the sequence in the originalList and tempList is going to be same.

If you are following above process then your test never fails, because When you change one list, it changes the other list as well.

How to check the options in drop down are sorted order or not

Step1: Create a List tempList variable
Step2: While iterating the option in the dropdown, add values to tempList (along with originalList)
Step3: Now sort the tempList, sorting of tempList will not affect the originalList because we have created two different objects
Step4: Compare the two Lists

import java.util.ArrayList;
import java.util.Collections;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class VerifyDropDownSortedOptions {

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "C://Drivers/chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();

driver.get("https://testautomationpractice.blogspot.com/");

driver.manage().window().maximize();

WebElement element = driver.findElement(By.id("animals"));

Select se = new Select(element);

ArrayList originalList = new ArrayList();
ArrayList tempList = new ArrayList();

for (WebElement e : se.getOptions()) {
originalList.add(e.getText());
tempList.add(e.getText());
}

System.out.println("this is originalList before Sorting" + originalList);
System.out.println("this is tempList before Sorting" + tempList);

Collections.sort(tempList);

System.out.println("this is originalList after Sorting" + originalList);
System.out.println("this is tempList after Sorting" + tempList);

if (originalList == tempList) {
System.out.println("Dropdown sorted");
} else {
System.out.println("Dropdown Not sorted");

}
driver.close();
}

}



Webservices API Testing


What is an API?

API is means Application Programming Interface.
It enables communication and data exchange between two separate software systems. 
A software system implementing an API contains functions/sub-routines which can be executed by another software system.

What is API testing?

API Testing is entirely different from GUI Testing and mainly concentrates on the business logic layer of the software architecture. This testing won't concentrate on the look and feel of an application.
Instead of using standard user inputs(keyboard) and outputs, in API Testing, you use software to send calls to the API, get output, and note down the system's response.
API Testing requires an application to interact with API. In order to test an API, you will need to 
Use Testing Tool to drive the API 
Write your own code to test the API






What is Web Service?

Web Service available over the web which Enables communication between applications over the web,
Provides a standard protocol/format for communication 

Why we use it?

Platform independent communication - using web services two different applications (implementation) can talk to each other and exchange data/information

Difference between API & Web service

Web Service is an API wrapped in HTTP.
All Web Services are API but APIs are not Web Services.
Web Service might not perform all the operations that an API would perform.
A Web Service needs a network while an API doesn't need a network for its operation.

What is WSDL?

WSDL stands for Web Services Description Language, an XML-based language that describes Web services and how to access and locate them.

What is UDDI?

UDDI stands for Universal Description, Discovery and Integration. It is an open, Internet-based specification that offers directory service for storing information about web services.

Types of Web Services

There are mainly two types of web services.

  • SOAP web services. (Simple Object Access Protocol) 
  • RESTful web services. (Representational State Transfer)


SOAP (Simple Object Access Protocol) – SOAP is a protocol which was designed before REST and came into the picture. The main idea behind designing SOAP was to ensure that programs built on different platforms and programming languages could exchange data in an easy manner.

REST – This was designed specifically for working with components such as media components, files, or even objects on a particular hardware device. Any web service that is defined on the principles of REST can be called a RestFul web service. A Restful service would use the normal HTTP verbs of GET, POST, PUT and DELETE for working with the required components.

HTTP V/S HTTPS

What Is HTTP?
HTTP stands for Hypertext Transfer Protocol. At it’s most basic, it allows for the communication between different systems. It’s most commonly used to transfer data from a web server to a browser in order to allow users to view web pages. It’s the protocol that was used for basically all early websites.

What Is HTTPS?
HTTPS stands for Hypertext Transfer Protocol Secure. The problem with the regular HTTP protocol is that the information that flows from server to browser is not encrypted, which means it can be easily stolen. HTTPS protocols remedy this by using an SSL (secure sockets layer) certificate, which helps create a secure encrypted connection between the server and the browser, thereby protecting potentially sensitive information from being stolen as its transferred between the server and the browser.



Software Testing Interview Questions and Answers


Q1.How do you define Bug and Defect?
  • Defect – This is what if software misses any function or feature which are there in requirement list, then this is known as a defect.
  • Bug – A bug is a flaw or a failure in a system that causes it to yield an unexpected or incorrect result.

Selenium Frequently Asked Questions & Answers Part-5




Ques.101. How can we run a Test method multiple times in a loop(without using any data provider)?
Ans. Using invocationCount parameter and setting its value to an integer value, makes the test method to run n number of times in a loop.

Ques.102. What is the default priority of test cases in TestNG?
Ans. The default priority of test when not specified is integer value 0. So, if we have one test case with priority 1 and one without any priority then the test without any priority value will get executed first (as default value will be 0 and tests with lower priority are executed first).


Ques.103. What is the difference between soft assertion and hard assertion in TestNG?

Ans. Soft assertions (SoftAssert) allows us to have multiple assertions within a test method, even when an assertion fails the test method continues with the remaining test execution.
The result of all the assertions can be collated at the end using softAssert.assertAll() method.
Here, even though the first assertion fails still the test will continue with execution and print the message below the second assertion.
Hard assertions on the other hand are the usual assertions prodived by TestNG. In case of hard assertion in case of any failure, the test execution stops, preventing execution of any further steps within the test method.
 

Ques.104. How to fail a testNG test if it doesn't get executed within a specified time?
Ans. We can use timeOut attribute of @Test annotation.
The value assigned to this timeOut attribute will act as an upperbound, if test doesn't get executed within this time frame then it will fail with timeOut exception.

 
Ques.105. How can we skip a test case conditionally?
Ans. Using SkipException, we can conditionally skip a test case. On throwing the skipException, the test method  marked as skipped in the test execution report and any statement after throwing the exception will not get executed.


Ques.106. How can we make sure a test method runs even if the test methods or groups on which it depends fail or get skipped?
Ans. Using "alwaysRun" attribute of @Test annotation, we can make sure the test method will run even if the test methods or groups on which it depends fail or get skipped.
Here, even though the parentTest failed, the dependentTest will not get skipped instead it will executed because of "alwaysRun=true". In case, we remove the "alwaysRun=true" attribute from @Test then the report will show one failure and one skipped test, without trying to run the dependentTest method.


Ques.107. Why and how will you use an Excel Sheet in your project?
The reason we use Excel sheets is because it can be used as data source for tests. An excel sheet can also be used to store the data set while performing DataDriven Testing.


Ques.108. How can you redirect browsing from a browser through some proxy?

Selenium provides a PROXY class to redirect browsing from a proxy. Look at the example below:


Ques.109. How to scroll down a page using JavaScript in Selenium?
We can scroll down a page by using window.scrollBy() function.
Example:
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)")


Ques.110. How to scroll down to a particular element?

To scroll down to a particular element on a web page, we can use the function scrollIntoView().
Example:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
 

Ques.111. How to set the size of browser window using Selenium?
To maximize the size of browser window, you can use the following piece of code:driver.manage().window().maximize(); – To maximize the window
To resize the current window to a particular dimension, you can use the setSize() method.


Ques.112. Can we enter text without using sendKeys()?
Yes. We can enter/ send text without using sendKeys() method. We can do it using JavaScriptExecutor.

 Ques.113. Explain how you will login into any site if it is showing any authentication popup for username and password?
Since there will be popup for logging in, we need to use the explicit command and verify if the alert is actually present. Only if the alert is present, we need to pass the username and password credentials.
The sample code:
 
Ques.114. Explain what is Group Test in TestNG?
In TestNG, methods can be categorized into groups. When a particular group is being executed, all the methods in that group will be executed.  We can execute a group by parameterizing it’s name in group attribute of @Test annotation. Example: @Test(groups={“xxx”})
 

Ques.115. How To Run Failed Test Cases Using TestNG In Selenium WebDriver
By using “testng-failed.xml”


Ques.116. What is Stale Element Exception? How to handle it?
Stale means old, decayed, no longer fresh.
Stale Element means an old element or no longer available element.
Assume there is an element that is found on a web page referenced as a WebElement in WebDriver. If the DOM changes then the WebElement goes stale. If we try to interact with an element which is staled then the StaleElementReferenceException is thrown.
When this happens you will need to refresh your reference, or find the element again.


Ques.117. What are different XPath functions that you have used in your Project?
Contains()
Using OR & AND
Start-with() function
Text()

Ques.118. What will happen in background when execute new FirefoxDriver() ?
Firefox binary will be triggered and Fiefox browser will open with default options.
FirefoxDriver object is created


Ques.119. What is the below statement means and Why?WebDriver driver = new FirefoxDriver();
WebDriver is an interface which contain several abstract methods such as get(...), findElamentBy(...) etc.
We simply create reference of web Driver and we can assign objects (Firefox driver, ChromeDriver, IEDriver, Andriod driver etc) to it.

Ques.120. How do you handle inner Frames and Adjacent Frames?
 SwitchTo frame1, SwitchTo frame2 (inner frame) work on the element and switchto default content
Use SwitchTo frame to move the control inside frame.


Ques.121. How to click on an element which is not visible using selenium WebDriver?

We can use JavascriptExecutor to click.


WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element); 



Ques.122. Difference between verify and assert?
Assert: Assert command checks if the given condition is true or false. If the condition is true, the program control will execute the next phase of testing, and if the condition is false, execution will stop and nothing will be executed.
Verify: Verify command also checks if the given condition is true or false. It doesn't halts program execution i.e. any failure during verification would not stop the execution and all the test phases would be executed.


Ques.123. What is the use of @FindBy annotation?
@FindBy is used to identify element in the Page Factory approach.


Ques.124. Do you use Thread.sleep?
Rarely


Ques.125. What are different pop-ups that you have handle in your projects?

JavaScript Pop
Alert alert = driver.switchTo().alert();
Browser Pop Ups
Browser Profiles, Robot Class, AutoIT, Sikuli
Native OS Pop Ups
Browser Profiles, Robot Class, AutoIT, Sikuli

API testing using POSTMAN


API testing using POSTMAN

The API Testing is performed for the application, which has a collection of API that must be tested. API calls verify functionality and expose failure of application.
API testing is strongly connected with Back-end/Database testing, you may have brief knowledge of SQL queries. (That would be an advantage )

How to Run Test Cases using Selenium Grid


Selenium Grid is a tool that distributes the tests across multiple physical or virtual machines so that we can execute scripts in parallel (simultaneously). It dramatically accelerates the testing process across browsers and across platforms by giving us quick and accurate feedback.
Selenium Grid allows us to execute multiple instances of WebDriver or Selenium Remote Control tests in parallel which uses the same code base, hence the code need NOT be present on the system they execute. The selenium-server-standalone package includes Hub, WebDriver, and Selenium RC to execute the scripts in grid.

Apache Pig Notes



What is pig?

Implemented by Yahoo.
Pig Hadoop echo system s/w from apache foundation used for analysing the data.
Pig uses pig latin language.

Apache Sqoop Notes


Overview on Sqoop

Sqoop is open source s/w from Apache used for transfer data between RDBMS(oRACLE, sLQSERVER, mYSQL...) and HDFS.

MySQL Database

Connecting to MySQL Database:
root user: root/cloudera
other user: cloudera/cloudera

Apache Hive Notes


Apache Hive
  • Hive is a data warehouse infrastructure tool to process structured data in Hadoop. 
  • Initially Hive was developed by Facebook, later the Apache Software Foundation took it up and developed it further as an open source under the name Apache Hive.
  • It stores schema in a database and processed data into HDFS.
  • It is designed for OLAP not for OLTP.
  • It provides SQL type language for querying called HiveQL or HQL.
  • Hive is not RDBMS.
  • Hive is a system for managing and querying un-structured data into structured format. It uses the concept of Map Reduce for execution.

Apache Hive External Tables


External tables

  • Data will be available in HDFS. The table is going to create on HDFS data.
  • We can call this one as schema on data.
  • At the time of dropping the table it drops only schema, the data will be still available in HDFS as before.
  • External tables provide an option to create multiple schemas for the data stored
  • in HDFS instead of deleting the data every time whenever schema updates

Apache Hive Complex Data Types(Collections)


Complex Data Types
  1. arrays: ARRAY
  2. maps: MAP
  3. structs: STRUCT

How to test Python MapReduce Jobs in Hadoop


Example:  Count Number of words in a text file (word count)
1) Create Python scripts mapper.py & reducer.py 

2) Test mapper.py and reducer.py scripts locally before using them in a  MapReduce job.  

Test1:
[cloudera@quickstart training]$ echo "abc xyz abc abc abc xyz pqr" | python /home/cloudera/training/wordcount-python/mapper.py |sort -k1,1 | python /home/cloudera/training/wordcount-python/reducer.py
abc      4
pqr      1
xyz      2

Test2:
[cloudera@quickstart training]$ cat wordcount.txt | python /home/cloudera/training/wordcount-python/mapper.py |sort -k1,1 | python /home/cloudera/training/wordcount-python/reducer.py
are       3
how     2
is         1
welcome         1
where 1
you      4

3)  Create ‘wordcountinput’ directory in HDFS then copy wordcount.txt to HDFS .
[cloudera@quickstart training]$ hdfs dfs -mkdir /wordcountinput
[cloudera@quickstart training]$ hdfs dfs -put wordcount.txt /wordcountinput

4) Execute MapReduce job using streaming jar file .
 Location: /usr/lib/hadoop-0.20-mapreduce/contrib/streaming

[cloudera@quickstart training]$ hadoop jar /usr/lib/hadoop-0.20-mapreduce/contrib/streaming/hadoop-streaming-2.6.0-mr1-cdh5.12.0.jar -Dmapred.reduce.tasks=1 -file /home/cloudera/training/wordcount-python/mapper.py /home/cloudera/training/wordcount-python/reducer.py -mapper "python mapper.py" -reducer "python reducer.py" -input /wordcountinput/wordcount.txt -output /wordcountoutput

5) Check the output 
[cloudera@quickstart training]$ hdfs dfs -ls /wordcountoutput
Found 2 items
-rw-r--r--   1 cloudera supergroup          0 2018-05-24 00:40 /wordcountoutput/_SUCCESS
-rw-r--r--   1 cloudera supergroup         41 2018-05-24 00:40 /wordcountoutput/part-00000

[cloudera@quickstart training]$ hdfs dfs -cat /wordcountoutput/part-00000
are       3
how     2
is         1
welcome         1
where 1
you      4



How to test Java MapReduce Jobs in Hadoop


Example-1 (wordcount)

Developer activities:
Step1: Develop MapReduce Code
Step2:  Unit Testing of Map Reduce code using MRUnit framework
Step3: Create Jar file for MapReduce code

Testing activities:
Step1:  Create a new directory in HDFS then copy data file from local to HDFS directory
[cloudera@quickstart training]$ hdfs dfs -mkdir /mapreduceinput
[cloudera@quickstart training]$ hdfs dfs -put wordcount.txt /mapreduceinput
Step2 : Run  jar file by providing data file as an input
[cloudera@quickstart training]$ hadoop jar wordcount.jar WordCount /mapreduceinput/wordcount.txt /mapreduceoutput/
Step3:  Check output file created on HDFS.
[cloudera@quickstart training]$ hdfs dfs -ls /mapreduceoutput
Found 2 items
-rw-r--r--   1 cloudera supergroup          0 2018-05-23 22:00 /mapreduceoutput/_SUCCESS
-rw-r--r--   1 cloudera supergroup         41 2018-05-23 22:00 /mapreduceoutput/part-00000
[cloudera@quickstart training]$ hdfs dfs -cat /mapreduceoutput/part-00000
are       3
how     2
is         1
welcome         1
where 1
you      4


Example-2 (Find out Number of Products Sold in Each Country)

Step1:  Create a new directory in HDFS then copy data file from local to HDFS directory
[cloudera@quickstart training]$ hdfs dfs -mkdir /productsalesinput
[cloudera@quickstart training]$ hdfs dfs -put SalesJan2009.csv /productsalesinput

Step2 : Run  jar file by providing data file as an input
[cloudera@quickstart training]$ hadoop jar ProductSalesperCountry.jar SalesCountry.SalesCountryDriver /productsalesinput/SalesJan2009.csv /productsalesoutput

Step3:  Check output file created on HDFS.
[cloudera@quickstart training]$ hdfs dfs -ls /productsalesoutput
Found 2 items
-rw-r--r--   1 cloudera supergroup          0 2018-05-23 23:52 /productsalesoutput/_SUCCESS
-rw-r--r--   1 cloudera supergroup        661 2018-05-23 23:52 /productsalesoutput/part-00000
[cloudera@quickstart training]$ hdfs dfs -cat /productsalesoutput/part-00000

Example-3 (MapReduce Join – Multiple Input Files)

Step1:  Create a new directory in HDFS then copy data file from local to HDFS directory
[cloudera@quickstart training]$ hdfs dfs -mkdir /multipleinputs
[cloudera@quickstart training]$ hdfs dfs -put customer.txt /multipleinputs
[cloudera@quickstart training]$ hdfs dfs -put delivery.txt /multipleinputs

Step2 : Run  jar file by providing data file as an input
[cloudera@quickstart training]$ hadoop jar MultipleInput.jar /multipleinputs/customer.txt /multipleinputs/delivery.txt /multipleoutput
Step3:  Check output file created on HDFS.
[cloudera@quickstart training]$ hdfs dfs -ls /multipleoutput
Found 2 items
-rw-r--r--   1 cloudera supergroup          0 2018-05-26 23:14 /multipleoutput/_SUCCESS
-rw-r--r--   1 cloudera supergroup         22 2018-05-26 23:14 /multipleoutput/part-r-00000
[cloudera@quickstart training]$ hdfs dfs -cat /multipleoutput/part-r-00000
mani   0
vijay    1
ravi      1

MRUnit test case for wordcount example

Pre-Requisites
Download the latest version of MRUnit jar from Apache  website: https://repository.apache.org/content/repositories/releases/org/apache/mrunit/mrunit/
mrunit-0.5.0-incubating.jar

Maven pom.xml dependency ( If you are using Maven Project)

org.apache.mrunit
mrunit
0.9.0-incubating
hadoop1 


Step1: Create a new Java project in Eclipse then add JUnit Library.

Step2:  Add external Jars which are required to run Junit test case
/usr/lib/hadoop  
 /usr/lib/hadoop-0.20-mapreduce
/home/cloudera/training/MRUnit/mrunit-0.5.0-incubating.jar
In Addition we need to also add wordcount.jar ( The classes from wordcount.jar  will be used in JUnit test case)

Step3:  Create Junit Test case

Word count MRUnit test case:
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mrunit.MapDriver;
import org.apache.hadoop.mrunit.MapReduceDriver;
import org.apache.hadoop.mrunit.ReduceDriver;
import org.junit.Before;
import org.junit.Test;

public class TestWordCount {
            MapReduceDriver mapReduceDriver;
            MapDriver mapDriver;
            ReduceDriver reduceDriver;

            @Before
            public void setUp() {
                        WordMapper mapper = new WordMapper();
                        SumReducer reducer = new SumReducer();
                        mapDriver = new MapDriver();
                        mapDriver.setMapper(mapper);
                        reduceDriver = new ReduceDriver();
                        reduceDriver.setReducer(reducer);
                        mapReduceDriver = new MapReduceDriver();
                        mapReduceDriver.setMapper(mapper);
                        mapReduceDriver.setReducer(reducer);
            }

            @Test
            public void testMapper() {
                        mapDriver.withInput(new LongWritable(1), new Text("cat cat dog"));
                        mapDriver.withOutput(new Text("cat"), new IntWritable(1));
                        mapDriver.withOutput(new Text("cat"), new IntWritable(1));
                        mapDriver.withOutput(new Text("dog"), new IntWritable(1));
                        mapDriver.runTest();
            }

            @Test
            public void testReducer() {
                        List values = new ArrayList();
                        values.add(new IntWritable(1));
                        values.add(new IntWritable(1));
                        reduceDriver.withInput(new Text("cat"), values);
                        reduceDriver.withOutput(new Text("cat"), new IntWritable(2));
                        reduceDriver.runTest();
            }

            @Test
            public void testMapReduce() {
                        mapReduceDriver.withInput(new LongWritable(1), new Text("cat cat dog"));
                        mapReduceDriver.addOutput(new Text("cat"), new IntWritable(2));
                        mapReduceDriver.addOutput(new Text("dog"), new IntWritable(1));
                        mapReduceDriver.runTest();
            }

}

Step4: Run JUint test case

Step5: Results should be passed.


Followers