QUICKLEARNINGAUTOMATION

AUTOMATIONCONCEPTS

Ads Here

Friday, September 30, 2022

SELENIUM AND JAVA COMBINED TOPICS




LOCATORS

we have three element First and Last name field and a link element with "Partial Link Text" as the Link text. Lets try to create a repository for these three elements. A property file stores information in a Key value ppair. 

In our case we have to store three values instead of two. Element Name,Locator Type and Locator value.


  • ClassName – A ClassName operator uses a class attribute to identify an object.
  • cssSelector – CSS is used to create style rules for webpages and can be used to identify any web element.
  • Id – Similar to class, we can also identify elements by using the ‘id’ attribute.
  • linkText – Text used in hyperlinks can also locate element
  • name – Name attribute can also identify an element
  • tagName – We can also use a tag to locate elements
  • xpath – Xpath is the language used to query the XML document. The same can uniquely identify the web element on any page.



How to capture the locators


                                   


OBJECT REPOSITORY

Reading Object Repository properties file

Java has support or the .properties files. One can easily read a properties file in three simple step

  1. Create a FileInputStream object on the .properties file
  2. Creating a Properties object over the File input stream created in step 1
  3. Simply read the Key-Values by using the getProperty("Property name"); method on Properties class.

For reading an Object Repository we will create a simple class and name it RepositoryParser.java. Here is the code for that





HANDLING MULTIPLE WINDOWS

It is a unique identifier that holds the address of all the windows.In this website when the user clicks a button ,an another window opens that is a child window.

Suppose if there are multiple tabs are opened,If you want to perform some operation in that particular window like capuring the locators for any child-window page,so that window will be active.

At last If I give the command

driver.close();

The child window which is active that one will get closed


In the above code ,Use stored all the child windows in the array list with the command 

"driver.getWindowHandles()"

If I execute the program


In the above program it counted how many windows are open.There are two windows open 

driver.switchTo().window(tabs.get(1)); driver.close();

Second window is opened and closed

User can switch to any window by using the command

  driver.switchTo().window(tabs.get(0));

This one is the current url and print it on the console

HANDLING FRAMES

Here to switch in to another frame we have to use:

driver.switchTo.defaultContent() and then we can switch in to any frame

driver.switchTo.frame(1);

Perform the action

driver.switchTo.frame(1);

driver.findElement(By.id("name")).sendKeys("Tanu);





METHOD OVERLOADING

In the method name stays the same, the compiler picks the right alternative depending on the actual argument values at the invocation point (the best example of overloading is constructors in Java: the name is always the same but the set of arguments is different) or raises a compiler error if none found. For example:




    In the above example if we specify the same method name with no arguments then it throws an error, but if other method has passes an argument then it is method overloading





CONSTRUCTOR


Constructor is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory.

It has two keywords:

  1. this keyword
  2. super keyword
this keyword mainly represents the current instance of a class.
this keyword used to access methods of the current class as it has reference of current class.
For Example:


or

ENCAPSULATION

In the above example ,By giving only this Keyword refers to only this particular class.The concept of encapsulation in object-oriented programming is all about hiding the implementation details (like state, internal methods, etc.) from the outside world. The benefits of encapsulation are maintainability and ease of change
Encapsulation in Java language means binding the data (variables) with the code(methods-setters and getters).
If setter is present and getter is not present,the variable is write-only, which means,other classes could only modify the value of the variable




In the above Test Base example we created only two methods ,First Method We are not passing any parameter and in the second method we are passing one parameter.

INHERITANCE

This is the common functionality for all the class files. In every test class we no need to specify chromedriver path or capturing screenshot .All you need to is extend this testbase class to another class


  • Inheritance is one of the key concepts of object-oriented programming
  • Inheritance allows designing extensible and maintainable class hierarchies.
  • Inheritance in Java is implemented using subclassing and the extends keyword, followed by the parent class.The subclass inherits all of the public and protected members of its parent class, a subclass inherits the package-private members of the parent class if both reside in the same package.

               

 Child Class / Sub Class : The class that extends the features of another class is known as child class, subclass, or derived class.


Parent Class / Superclass : The class whose properties and functionalities are used(inherited) by another class is known as a parent class, superclass.

So,In the Addcustomer test user is trying to call the functionalities from the Test Base class to AddCustomer Test class by using the extend keyword.




In the below example we have used the constructor overloading concept, and we have created an object of child class and after calling the constructor of child class the first line in it is super(p) which says that call the matching constructor from the parent class, if we do not mention that line, by default it calls the super() with no parameterized constructor from Parent class.


If setter is not present and getter is present, the variable is read-only, which means, other classes can only read value of the variable, but cannot modify its value.

Constructor does not support Multiple Inheritance, Java does not support multiple inheritance: in Java every class has exactly one direct parent (with Object class being on top of the hierarchy as we have already known from part 2 of the tutorial, Using methods common to all objects). However, the class may implement multiple interfaces and as such, stacking interfaces is the only way to achieve (or mimic) multiple inheritance in Java. 


Implementation of multiple interfaces is in fact quite powerful, but often the need to reuse an implementation leads to deep class hierarchies as a way to overcome the absence of multiple inheritance support in Java.


UPLOAD A FILE
       
AUTOIT


            For some Applications like Insurance or Health Care .In google Generally the users can easily upload their medical records from the computer but In selenium we have to use the AutoIT write the script and to upload the file from the computer.To do this:

We need to use two tools

  • SciTE
  • AutoIT Window Info(64)
Just like Chropath where we can capture the locators  from Web Applications.For Windows Applications also there is a tool called
AutoIT Window Info64.In AutoIT With the help of "Find Tool" button 
We can able to identify the field name and have to write the script

AUTOIT

SCITE


UPLOAD A FILE FOR WINDOWS BASED APPLICATIONS USING ROBOT



SIKULI

  • Download sikuli jar file Add jar file 
  •   First,Take a screenshot of File Name by using Snipping tool


  • Second ,take a screenshot of Open button Using Snipping tool


By initialising Screen s=new Screen();

Images for windows file input text box and open button are stored onto ‘FileTextBox.PNG’ and ‘OpenButton.PNG’.

The next statements include the creation of objects for Screen and Pattern classes. Create a new screen object. Set the path of the file you want to upload as a parameter to the Pattern object.

Screen s =new Screen();
Pattern fileInputTextBox = new Pattern("C:\\Users\\User\\Desktop\\Captur1.png");
Pattern openbutton = new Pattern("C:\\Users\\User\\Desktop\\Captur2.png");

 Wait for the windows popup to appear.

Type the file path onto input file text box and click on Open button





What is the Stale Element Exception


  While exceuting selenium searches for a particular webelement and that element has not been present in the webpage or has been deleted entirely.So what is the solution now

You can refresh the page and again try for the same element.

Example

driver.navigate().refersh();

driver.findElement(By.id(“ur element id”)).click();

 Alert

a small box that appears on the display screen to give you some kind of information

Some times we use to get some pop-up messages or notification

To avoid the notification, We can use

alert.dismiss;

(or)

After successful signup the page,it will give you confirmation message pop-up

alert.accept




After executing the program,User faces the "no such alert" exception




User gets an error exception "no such alert",It is because selenium couldn't understand which element it is? and what to click?.So we need to create an WebElement and capture the xpath then we have to click particular element




SELENIUM WAITS


  • Implicit waits are used to provide a default waiting time between each consecutive test step/command across the entire test script. Thus, the subsequent test step would only execute when the specified amount of time has elapsed after executing the previous test step/command.
  • Explicit waits are used to halt the execution until the time a particular condition is met or the maximum time has elapsed. Unlike Implicit waits, Explicit waits are applied for a particular instance only.They allow your code to halt program execution, or freeze the thread, until the condition you pass it resolves.The condition you pass it resolves.The condition is called with a certain frequency until the timeout of the wait is elapsed. Do not mix implicit and explicit waits
  • Doing so can cause unpredictable wait times.For example, setting an implicit wait of 10 seconds and an explicit wait for 15 seconds could cause a timeout to occur after 20 seconds.
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));



At first You need to import these two 

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


            


DYNAMIC WEB TABLE

How to capture the dynamic xpath in Web Table
Friends,It is very easy to findout the xpath of dynamic webtable

First capture the entire table and findout the id of the table and then tbody(1) table body first ,second row td[2] and third column td[3]



PROGRAM:



OUTPUT




JIRA INTEGRATION WITH SELENIUM

In Manual Testing we use to write the bug summary,bug description and bug Id manually.We are Automation testers and we have to everything by automation but how:

Yes,Its possible through Selenium Integration with jira




FIRST STEP:
When Selenium executes  and script has  any error automatically it will raise a bug ticket by using Jira Retention Policy




Secondly,Create a JiraServiceProvider class then we have to add the fields like  Issue Type,Summary ,Description,Reporter Name in Jira


Now Comes the Actual Automation,So far Selenium knows how to raise a bug ticket whenever failure occurs in the selenium script with all the required fields Bug summary,description and Bug Id

But the main point is Selenium has to integrate with Jira . We need to have URL,jira email,password and Project Name

jiraserviceprovider jiraSp = new jiraserviceprovider("https://sireecxyz.atlassian.net/",
"tanujaanvit@gmail.com", "k8Ma7ME34x38XyUbFMEa367A", "NK");


Then we have to get the method from the Jira Service Provider

jiraSp.createJiraTicket("Bug", issueSummary, issueDescription, "Tanuja");

In the above program we are calling ITestListener

                                INTERFACE

 we are calling abstract method ITestListener by using implementing keyword we are getting all the methods from the listener file like "Pass","fail" and "Skip" this process is called the interface.After implementing all the methods we can override that means we can write our own code






Just add the annotation for the negative testcases

When you execute the script from testng.xml,If the test script fails then it automatically create a bug ticket with all the required fields and send it to jira.

In this way others can verify whether the jira report




Two – dimensional Array (2D-Array)


Two – dimensional array is the simplest form of a multidimensional array. A two – dimensional array can be seen as an array of one – dimensional array for easier understanding. 

Indirect Method of Declaration:

  • Declaration – Syntax:
data_type[][] array_name = new data_type[x][y];
        For example: int[][] arr = new int[10][20];
DataProvider method which returns a two dimensional array.
Suppose you are testing a student registration application. To register a student, you need to pass Name, Address and City of student. You need to understand here that You required three  data

Row is a Number of students to be registered which is three and each row has three columns which are details of each student. Below is the flow it goes through:
 Now use first column (Name) of first row and use it followed by second column (Address) and third column (City). Done with first row.Now you will pick second row and iterate through its columns and same for each columns of each rows.
In a DataProvider method in TestNG.We need to pass above table in form of an Array to method from a DataProvider method.We will create a 2D(Two Dimensional Matrix) array containing rows and columns which looks similar to above table.I hope you would have basic understanding of Arrays.




In the above program we are passing the parameters that field names are from the excel sheet through the String and get the rows and columns.


OUTPUT:






















No comments:

Post a Comment