Selenium Class Notes
Selenium Class Notes
Automation:-
The process of converting the manual test cases to test scripts with the help of some
automation tool is known as automation. These scripts can be executed on the targeted
application when ever needed.
Advantages:-
We can save the test execution time( regression time). On and average to save around
50-60% of the test execution time.
We can reduce CTC(Cost To The Company).
The test scripts are repeatable on multiple builds.
We can maintain accuracy by executing the same scripts in the same manner.
Bugs can be identified.
The tool will provide the test report to analyse the bugs and we can ensure for quality.
What is the difference between selenium and QTP? (Or) Why you
have selenium for your project?
Note:-
Selenium is for web and mobile applications. If any desktop components or fields available in
the application cam handled with the help of SIKULI or AUTOIT.
Selenium components:-
The selenium is majorly built on below components.
Selenium IDE:-
IDE stands for Integrated Development Environment
It is to record and run the script.
It is an add-on for Firefox & Chrome(we can install and record in Firefox & Chrome
browser only).
It is accountable for user actions(for each and every action it will generate the script).
The recorded scripts can be run against other browsers also with the help of RC or
web driver.
Selenium RC:-
RC stands for Remote Control.
It is the selenium1 and introduced in 2004 by Jason huggins.
It is a server which is responsible to launch all the browsers but one browser at a time.
We can able to automate the entire application by using selenium RC.
Selenium Grid:-
It is for parallel execution. We can launch all the browsers parallel and the scripts can
be executed in all the browsers parallel at a time.
Selendroid:-
It is to test the mobile apps on the android platform. it is to test on less than 4 android
versions
Appium:-
It is to test the mobile applications on both android and iOS platforms. If the android
version is more than 4 we need to use Appium
Describe the steps to record the scenario like open google.com type selenium4testing in the
test box then click on Google search
Give the project name as Google
Give the URL as http://google.co.in
Then click on Start Recording
Perform actions on the browser
Selenium ide will capture all the actions
Click on Stop recording option to stop the recording
Provide test name as Googlesearch
To run click on the option play button
Select Tests
Click on + icon to take new test case
Provide test case name as Test2
Click on Rec option to record
Once the recording is completed then use the option “Run all tests” to execute all the
test cases
When to go for scripting and when to go for recording for a project automation?
To automate any project we have to use the scripting by using webdriver while
scripting if you are facing any issues then it can be overcome by using selenium
IDE.
Eclipse
JDK (8)
https://www.eclipse.org/downloads/packages/release/oxygen/3a
https://selenium.dev/downloads/
Webdriver Setup
Take the eclipse and extract the zip file.
Copy to any desired drive.
Check that JDK 8 is available in c:/programme file/java. If is not available then install
JDK.
Create a folder with a name lib/library copy the selenium server standalone jar file and
paste it in the library folder.
Go to eclipse then double click on blue icon (eclipse.exe) continue to open.
By default if displays some path in the workspace if required we can update the path.
Workspace is the folder where it contains the test scripts. Click Launch in workspace
launcher window.
Click on workbench to navigate into eclipse
Create a project
Navigate to file, click on new and select Java-project.
Give the project name as selenium automation.
Check that java SE 1.8. From the field ‘use an execution environment JRE’ then click on
finish.
Expand the selenium automation project
Right click on JRE system library, click on build path then select configure build path.
Click on add external Jars and select selenium server standalone jar file from the library
folder.
Click on Apply and Apply&Close.
Package:-
Package is nothing but a folder where it contains the classes .The package will be created
under the SRC folder of the selenium project.
Class:-
Class is a blue print or template. it contains objects.
Object can be anything which should be physically available.
In java, the object is nothing but the instance of the class.
Scenario2:-write a program to test the login functionality of hms by using the credentials
user1 and user1
Package: webdriver
Classname: HMS_Login:-
Take a new class by right clicking on the project ,click on new and select class.
Give the pacage name as Webdriver, And give the class name as HMS_logn
,thencheck the checkbox public static void main then click on finish
To perform action on any field we need the locator.we can To take the locators for
every browser we are having the below addon.
To take thelocator,right click on the field click on inspect element(Q),then use either
ID or Name or any locator.
Browser Addon
Fire fox inspect element
IE developer tools
Google chrome inspect element
Safari inspect element
Opera inspect element
Package: webdriver
Classname: Yahoo_Login
---------------------------------------------------------------------Add content
Package: webdriver
Classname: OpenURL_Selenium4Testing
Classname: facebook_login
Classname: HMS_PR_REG_para.java
driver.findElement(By.linkText(“manualFAQs”)).click();
driver.findElement(By.linkText(“seleniumFAQs”)).click();
driver.findElement(By.name("name")).click();
driver.findElement(By.Xpath("Xpath")).click();
//Upload a file
driver.findElement(By.name("image")).sendkeys(“path”);
In java when we ever we are going to give path then we have to use double
slash(//).
Classname: HMS_PR_REG_para.java
Scenario8:- write a program to login in to HMS and click on ADT then enter all the
values in new admition fields
Classname:GmailLogin
Synchronization:-
The process of providing wait to the tool to main the insyncbetween both application and
tool is known as synchronization. It is two types.
1. page load
2. page refresh
Page load:-
If the application is taking time to load the page then the webdriver by default handles
it. Not necessary to add any command
Page refresh:-
If the application taking time to refresh the page then we need to handle with the help
of below command.
Selenium thread.sleep(time);
(or)
Implicit/Explicit wait
It is a standard wait its simply waits upto the given time..
Thread.sleep is form of java command. Whenever we use it may through the
exception.
To handle it add throws exception to the method as well as to the main method.
Classname:GmailLogin
Scenario11:-Write a program to verify the one way search from Hyd to banglore in
spicejet
Refer the Programs from selenium4testing.com/downloads
Classname:Spicejet_Search
We are also having class datatype as non primitive datatypes like Strings&arrays
String:-
It is a class data type where we can store multiple characters. We can declare the
string(object) in 2 ways.
1. locally
2. globally
(To comment multiple lines select multiple line ctrl+ shift+ backward slash )
Local declaration:-
If we declare the variableslocally with in any block/ method then the scope of that reference
variable is limited to thatblock only. Local variables cant be used outside of the block.
Global declaration:-
If you are planning to access the object(data types, methods and instances ) in multiple
classes and methods then we need to declare it globally by using access modifiers like
private, public, protected, default.
Private:-
These objects can be used with in the class only. Private objects cant be used outside of the
class.
Public:-
These objects can be used within the classes as well as outside of the class. Public object can
be used with in the project of multiple classes& packages
2) Static
package basics;
publicclassCoreJavaBasics {
private String str; // Global Declaration
// public String str;
publicvoid sample1(){
// String str; // Local Declaration
str="Mahesh";
System.out.println("Welcome "+str);
}
publicvoid sample2(){
//String str1="Rajesh";
str="Rajesh";
System.out.println("Welcome "+str);
}
publicvoid sample3(){
//String str2="Ramu";
str="Ramu";
System.out.println("Welcome "+str);
}
publicstaticvoidmain(String[] args) {
// TODO Auto-generated method stub
CoreJavaBasicsc=newCoreJavaBasics(); //Object or
Instance
c.sample4("Rajesh");
c.sample4("Suresh");
c.sample4("Nagesh");
}
}
Refer the Programs from selenium4testing.com/downloads
Classname:
Parameterization:
The process of sending/passing the values(parameters) to the method from the main method
is known as parameterization.
Syntax:-
Classname:Selenium4Links_parameter
Classname:SpicejetSEarch_Parameter
How to handle java scripts alerts messages and pop ups or child windows?
Verify the one way search in spiecejet.com with more infont count then adults. Is
displays and java script alert messages click on it.
Before clicking on the alert message print the message which is available on the alert.
To handle the alert we need to navigate from the browser to the alert then we can
perform the actions either ok or cancel.
Accept( ):-
It is to click on dialogue alert and confirmation alert.
Syntax:-
Driver.switchto( ).alert ( ).dismiss( ); // to click on ok.
Get text:-
It is to take the test or label from the application. Where the web driver is having the focus.
String str;
System.out.println(str);
Scenario15:-
Write a program to verify the one way search in speicejet.com.
Then click on feild flights it will navigate to the select then click on currency
convertor.
where it displays a pop up then navigate to the pop up perform actions on pop up then
close it.
Select any value from the going to field.
To handle the pop up we need to navigate from the main window to the pop up by
using below command where we have to pass the window name.
Syntax:-
Driver.switchTo( ).window(“window name”);
driver.SwitchTo().window(“windowname”);
To navigate back from the pop-up to main window again we have to use the above
command whare we need to pass the main window name.
Syntax:-
String str;
Str=driver.getwindowHandle();
Driver.switchTo().window (str);
Scenario16:-
Write a program to login in to HMS,click on feedback,enter the valus in the pop-up,close the
pop-up by using driver.close() then click on Registration link on the main window.
Classname:
Chrome
IE
Safari
Opera
http://docs.seleniumhq.org/download/
Note:-
If you are facing any problem in IE.
To run the script IE. We have the use commands called “*iehta”.
If IE is blocking the execution then do the below execution setting in IE.
Go to tools,
Then click on internet options,
Then click on security,
Then click on uncheck the check box enable protected mode in all the
4 sections like internet,trusted sites, restricted sites,
Then click on apply,
Then click on ok,
Then again click on ok.
Classname:AllBrowsers
Estimations:-
The number of days which we are going to take to automate the identified test cases is known
as automation estimations
on and average we can able to automate around 2 to 3 test cases. It means per one testcase we
are taking around 3 to 4 hours.
Because ,
CSS:- (cascade style sheet)it will identify the element with the help of tagname.classname
If the classname is having any space then replace the space with dot(.)
Syntax:-
Driver.findelement(by.cssselector(“locator”)).click();
Absolute X-path:-
If we are unable to identify the element by using relative X-path due to same code
duplication, then we can go for absolute X-path. It will identify the elements with the
hierarchical path of the logs. We can take the absolute X-path from the below tools.
1. Fire path.
2. Copy X-path.
Fire path:-
It is an add-on for firebug. So we need to install firebug then we able to use fire path.
http://addons.mozilla.org/en_us/firefox/addon/firebug/
http://addons.mozilla.org/en_us/firefox/addon/firepath/
By default it will the relative X-path. To get the absolute X-path click on the arrow mark at
fire path and select generate absolute X-path close fire bug and capture the X-path again to
get the absolute X-path.
Copy X-path:-
It is an add-on for the browsers like Firefox, googlechrome, safari and opera. It will be
available by default in the above browsers.
What is the difference between the double slash ( // ) and single slash ( / ) in
the X-path?
//- represents the parent tag.
Debugging:-
The process of running the scripts step by step mode is known as debugging.
Debugging is to identify the errors in run time and also we can see the execution flow
of the scripts.
Framework: -
The structural way to implement the scripts for better maintenance is known as framework.
Maintenance means how we are implementing the scripts. Is it easy to analyse,
update, execute and getting the test report.
We have the below frameworks
1. Modular framework.
2. Data driven framework.
3. Keyword driven framework.
4. Hybrid framework.
Modular framework:
It is the basic framework and also known as linear framework.
The application will be divide into multiple modules and sub modules.
The automation engineer will implement the scripts for all the modules and sub
modules. once all the scripts are stable then they will integrate in a hierarchal order to
execute.
Advantages:
1. If any change in the application,we can update the change in the external file. So,
the maintenance is easy.
2. The driverScript will be implemented based on the common fields available in the
application. So it is reusablefor all kinds of applications.
3. If the application contains more number of fields like 100 textboxes, 100 buttons
etc. Then not necessary to implement those many lines of code, we can prefer data
driven driver script, which works for all the fields.
4. We can perform effective retesting
Disadvantages: -
1. Developing the driver script is a challenging task.
2. Taking all the fields locators into the excel file is also a challenging task.
Note: If the application is stable, but we need to test with multiple sets of different data then
we can prefer data driven framework.
Keyword driven framework:
Precondition Before
Note: The test engg.. will implement all the test cases in the form of steps (units / Keyword /
steps) then these will be executed from the Excel or XML
Advantages:
1. The same manual testing format we are using in automation, so the maintenance is
easy.
2. Junit and TestNG are predefined frameworks not necessary to implement the
frameworks.
3. Once the execution is completed it will provide the results also.
4. We can prefer the keyword driven fw for unstable applications (Agile based
projects)
Hybrid framework:-
1. Base Class: Contains all reusable methods like sendkeys (), click ().
2. OR (Object Repository): Repository means storage place or
Storage Folder. It contains all the field Locators.
3. TestData: The data which we are using for testing is known as
TestData. We will use XL file to store test data.
4. Components: Test cases will be implemented in the form of methods
nothing but those are Components.
5. Test Scripts: The components or Testcases will be executed in the form
of end to end flows.
6. XML File: Will use XML file to execute one class or multiple classes
(Batch execution) and also we can perform parallel execution.
7. Test Report: Once the executioncompleted the frame work will provide
report (results) in HTML Format.
8. Hybrid framework is structured as below:
Junit:
Junit is nothing but java unit testing framework. It is to implement and execute the scripts in
java programming language.
Scenario18:
Write a program to click on manual FAQ’S and selenium FAQ’S by using web driver with
Junit.
Package: junit
Classname:Selenium4Links_Junit
Step1:Takea Junit class by right clicking on the project, click on new and select Junit
test case.
Step2: Give the package name as Junit and give the class name as selenium4 links then check
the checkboxes setup and teardown .then click on finish.
Step3: Junit contains multiple annotations like @before, @test, @after etc.
@before:- It contains setup method where it acts as a precondition. When we run the script it
will be executes initially.
Once the execution is completed it will provide the result under the Junit section of
eclipse. If any exception/error identify it will be displayed under the failure trace.
Scenario19:
Write a program to perform the oneway search in spicejet.com by using webdriver with Junit.
Package: junit
Classname:SpicejetSearch_junit
Scenario20:
Write a program to perform permenant registration in HMS by using webdriver with Junit.
Package: junit
Classname:HMS_Reg_Junit
Scenario:-
Implement few scenarios and perform batchexecution .
Once all the scenarios are implemented right click on any class then click on
run as and select run configurations.
Click on the radio button run all test in the selected project then click on run
and click on run.
It executes all the classes in the alphabetical order.
Limitations of Junit:
For each and every test the before and after are applicable. So ,we need to implement
all the scenarios very independently.
Once the execution is completed it will provide the results under the Junit section of
eclipse but not in any external file (Excel or HTML).
To perform batch execution we are having the only one option like run/test in the
selected project. But it doesn’t have the option to run selected classes and selected
methods (scenarios).
It is having very limited annotations like
@before class
@ Before
@Test
@After
@After class
All the above limitations can be overcome by using “TestNG”.
--------------------------------------------------Julay 14-----------------------------------------
Scenario:-
Login into HMS then click on the feedback link then navigate to child
window(popup).
Enter same values in popup then close the popup, then click on ADT in the main
window.
Scenario:-
Login into bugzilla by using the credentials.
Jan 30 selenium@gmail.com password is selenium.
Then click on search, then click on advance search, then select ERP, HMS, Speicejet
in the product list box.
TESTNG:
The limitations which are identified in Junit are overcomed in testNG & also given added
advantages.
Click on Help in Eclipse then click on Install new software then click on add then Name: testNG
Location: http://beust.com/eclipse
then click on OK then expand TestNG & click on TestNG then click Next & again click on Next
& then click on radio button to accept the license & then click on finish.
Take a testNG class by right clicking on the project ,click on testNg, create TestNg class, click on
Browser at source folder, expand selenium automation project, select src then click on OK.
Give the package name as testNG & give class name as Selenium4Links, check the check boxes
@Before test, @After test & click on Finish.
once the execution is completed it will provide the results in two formats.
1. In console
2. It creates an html file under the project folder of bug space.
write a program to check on links like Hindi, Bengali, Telugu by passing parameters to
the method in TestNG with Web driver.
https://commons.apache.org/proper/commons.io/download
Advantages:-
If any test case failed to analyse the fail result we need a screen shot.
Its not possible to automate GUI testcases because, there is (ROI) return on
investiment at the same time its possible to implement the scripts for look and feel.
Scenario:-
Write a program to capture the screen shot for manual FAQs, selenium FAQs, general
testing FAQs?
Scenario:
Write a program to check the login functionality of Gmail by using TestNg with
Webdriver.
(Note: We can provide the description to the method as below:
Once the execution completed it will show the description in result file.
System.out.println(time);
File f= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
XML:-
Our plan is to use the XML file to perform the test execution.
Advantages:-
We can execute the test methods in user defind order (we can avoid alphabetical order
issue).
We can provide user defind name to default suite folder and default test html file.
We can perform batch execution.
We can perform parallel execution also.
<testname="Bugzilla">
<classes>
<classname="testNG.Bugzilla"></class>
</classes>
</test>
</suite>
XPATH:
By using xpath we can identify the field with available attributes & text along with
tag name.
Relative xpath:
Syntax: //tagname[@id=’test’]
//tagname[@value=’text’or @class=’name’]
NOTE: Due to the duplication if the relative xpath is not working then we can use
absolute xpath.
If any new update in the tags(source code) the existing absolute xpath may not work.
Exception Handle:
While we execute the script if any any line of code is failed,then the script execution will be
braek/skip.
The process of continuing the execution while exceptions/errors is known as Exception
handling.
We can acheie by using try,catch,block.
Syntax:
try
{
Driver.find element();
}
catch (IOException e)
{
System.out.println("you have given wrong input");
}
For Alert:
String str=null;
str=driver.switchTo().alert().getText();
System.out.println(str);
To handle alert we need to navigate the switch to the alert then we can perform the actions like
OK/CANCEL.
Inheritance:-
It is from OOPS.
The process of extending / reusing the objects from the parent class to child class is known as
inhertance
After inheritance the child class becomes combination of both parent and child.
Syntax:
Information Commands:
It is to take the information from the application.
get Text();_____Text means which is avaliable outside of the tag.
get Attribute();_____Which is avaliable inside the tag.
get All Options(); ______Take all the values from the dropdown/list box.
get All Selected Options();______Take only selected values from the list box.
getFirstSelectedOptios();________Take only selected values from dropdown.
Scenario:
Write a program to print gmail link displayname,Google search display name & tool tip off.
Package
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
@Test
public void f() {
String str = driver.findElement(By.linkText("Gmail")).getText();
System.out.println(str);
String str1=driver.findElement(By.name("btnK")).getAttribute("value");
System.out.println(str1);
String str2 =
driver.findElement(By.xpath("//*[@id='hplogo']/a/img")).getAttribute("title");
System.out.println(str2);
}
@BeforeTest
public void beforeTest() {
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//Global wait
driver.get("http://google.co.in");
}
@AfterTest
public void afterTest() {
driver.quit();
}
String comparisons:-
we can compare the string with another string by using the below commands.
Syntax:
String splits:
String contains one,two,three,four check that three is available or not. it available print it.
Splits:
Split is a method from the string we can divide the string to multiple values by using some
regular expression (some value). The value which we are using to divide the string it will be
removed from the string and the remaining values will be store in an array.
The return type of split is an array.
Array:
Array is a class data type where we can store multiple values with similar data types.
Array will store the values in a container format. So, for each value it will provide a
separate position.
int k[ ];
char c[ ];
object o[ ];
Array
0 one
two
1
three
2
four
3
scenario:-
login into HMS then click on permanent registration. print all the mandatory fields
which are available in PR phase check that last name and age fields are available (or)
not.