A command is what tells Selenium what to do.
Selenium uses what is called locators to find and match the elements of your page that it needs to interact with.
Element Locators tell Selenium, which HTML element a command refers to. Selenium webdriver generally uses 8 locators to find the elements on a web page. The following are the list of some object identifier or locators supported by selenium.
The format of a locator is: locatorType=argument
Example: – (“link=click”)
Selenium supports the following strategies for locating elements:
id=id
Select the element with the specified @id attribute
name=name
Select the first element with the specified @name attribute.
• username
• name=username
xpath=xpathExpression
Locate an element using an XPath expression.
• xpath=//img[@alt=’The image alt text’]
• xpath=//table[@id=’table1′]//tr[4]/td[2]
link=textPattern
Select the link (anchor) element which contains text matching the specified pattern.
• link=The link text
Here, we can see the code for simple login and logout functionality with comments and the explanation of the code.
Here is the sample script which will open a page…
This script was written using JUnit…
import org.junit.After; //Junit package
import org.junit.Before; //Junit package
import org.junit.Test; //Junit package
import org.openqa.selenium.server.SeleniumServer; //Selenium server package
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;
public class Google1 extends SeleneseTestCase { //Class name[class name should be same as the program name]
private Selenium selenium;
private SeleniumServer seleniumServer;
@Before
public void setUp() throws Exception { //Set UP method to start selenium and selenium server
/*Defining the selenium method.
DefaultSelenium selenium=new
DefaultSelenium(“localhost”,4444,”*iehta”,”https://”);
localhost—serverHost
4444 — serverport
Iehta(IE) or iexplore(IE) or firefox—– browser start command(we can use *firefox also)
https:// —browser URL(Here we can give full URL of site) */
selenium = new DefaultSelenium(“localhost”, 4444, “*iehta”, “https://”);
seleniumServer = newSeleniumServer();
seleniumServer.start();
selenium.start();
}
@TEST —//Our actual test case steps come here
public void testText1() throws Exception {
selenium.open(“https://www.yahoo.com”);
selenium.windowMaximize();
}
@After // To stop selenium and selenium server
public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}
}
Basically JUnit is an open source unit testing tool and used to test small/large units of code. To run the JUnit test you don’t have to create class object or define main method. JUnit provide assertion library which is used to evaluate the test result. Annotations of JUnit are used to run the test method. JUnit is also used to run the Automation suite having multiple test cases.
JUnit is otherwise, a unit testing framework which mostly used for unit testing of Java projects. JUnit assumes that all test methods can be performed in an arbitrary order. Therefore, tests should not depend on other tests.
To write a test with JUnit
Checkout Selenium Interview Questions
Text box:
selenium.type(“locator”, “text”);
Type:
Sets the value of an input field, as though you typed it in.
Parameters:
locator an element locator
value the value to type
Link/button/Check box/Radio button:
selenium.click(“link=linktext”);
Clicks on a link, button, checkbox or radio button. If the click action causes a new page to load (like a link usually does), call waitForPageToLoad.
Parameters:
locator an element locator
a) Select value:
selenium.select(“locaotr”, “lable=value”);
Select an option from a drop-down using an option locator.
Parameters:
selectLocator an element locator identifying a drop-down menu
optionLocator an option locator (a label by default)
b) Display all values:
selenium.getSelectOptions(selectLocator)
Gets all option labels in the specified select drop-down.
Parameters:
selectLocator an element locator identifying a drop-down menu
Returns:
an array of all option labels in the specified select drop-down
Multiselect ??List box
selenium.addSelection(locator, optionLocator);
Add a selection to the set of selected options in a multi-select element using an option locator.
Parameters:
locator an element locator identifying a multi-select box
optionLocator an option locator (a label by default)
selenium.removeSelection(locator, optionLocator);
Remove a selection from the set of selected options in a multi-select element using an option locator.
Parameters:
locator an element locator identifying a multi-select box
optionLocator an option locator (a label by default)
Capture screen shot(positive):
selenium.captureScreenshot(filename);
Captures a PNG screenshot to the specified file.
Parameters:
filename the absolute path to the file to be written, e.g. “c:blahscreenshot.png”
selenium.captureEntirePageScreenshot(filename, kwargs);
Saves the entire contents of the current window canvas to a PNG file. Parameters:
filename the path to the file to persist the screenshot as. No filename extension will be appended by default. Directories will not be created if they do not exist, and an exception will be thrown, possibly by native code.
kwargs a kwargs string that modifies the way the screenshot is captured. Example: “background=#CCFFDD” . Currently valid options:
background the background CSS for the HTML document. This may be useful to set for capturing screenshots of lessthan- ideal layouts, for example where absolute positioning causes the calculation of the canvas dimension to fail and a black background is exposed (possibly obscuring black text).
Capture screen shot(with failure)
public Scroll()
{
setCaptureScreenShotOnFailure(true);
}
selenium.getEval(javascript);
Gets the result of evaluating the specified JavaScript snippet. The snippet may have multiple lines, but only the result of the last line will be returned.
Parameters:
script the JavaScript snippet to run
Returns:
the results of evaluating the snippet
Page scroll down using Java script:
selenium.getEval(“window.scrollBy(0,500)”);
Scrolls down the page 500 pixles.
Alerts
Normal alerts
selenium.getAlert();
Retrieves the message of a JavaScript alert generated during the previous action, or fail if there were no alerts.
Returns:
The message of the most recent JavaScript alert
On load alerts: This can be handled by using Selenium 2
Handling Multiple windows and popups
Extracting window IDs with Selenium :
selenium.getAllWindowIds();
Returns the IDs of all windows that the browser knows about.
Returns:
the IDs of all windows that the browser knows about.
selenium.getAllWindowNames();
Returns the Namess of all windows that the browser knows about.
Returns:
the NAMESs of all windows that the browser knows about.
selenium.getAllWindowTitles();
Returns the Titles of all windows that the browser knows about.
Returns:
the Titles of all windows that the browser knows about.
Tabbed browsing and handling tabs with selenium RC Handling popups/new windows, performing actions in pop up window Closing pop up windows
The first thing we will do is configure Eclipse to let us see the source code for the Selenium Driver.
You should see a Class File Editor window, with the message “Source not found”. This is because we haven’t tol d Eclipse where the source code for the Selenium Java Driver and Server resides on the disk.
We can easily fix this by pressing the [Attach Source. . . ] button and navigating to the source code .jar file
• selenium?java?2.X?srcs.jar
With Eclipse setup like this, we can start to understand the code that we are using. It is very important to understand as much as
possible about the libraries and classes that you are using in your testing. You can start to understand the limitations or restrictions
of these classes and learn what you have to build yourself, and what comes for free in the short term to help you get started. If
you are just learning programming then you have a whole set of relevant source code to study to look at how other people have
used and built the tools. Reading source code is one of the easiest ways for a novice programmer to advance their programming skills.
Checkout Selenium Online Tutorial
To help you find the line number you can:
• switch on line numbering by right clicking on the left margin of the code window (where you set breakpoints) and select “Show line numbers”
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Selenium Training | Nov 19 to Dec 04 | View Details |
Selenium Training | Nov 23 to Dec 08 | View Details |
Selenium Training | Nov 26 to Dec 11 | View Details |
Selenium Training | Nov 30 to Dec 15 | View Details |
Madhuri is a Senior Content Creator at MindMajix. She has written about a range of different topics on various technologies, which include, Splunk, Tensorflow, Selenium, and CEH. She spends most of her time researching on technology, and startups. Connect with her via LinkedIn and Twitter .