To type a word in text box or text area, we have to use the below command.
Syntax:
Sets the value of an input field, as though you typed it in. Can also be used to set the value of combo boxes, check boxes, etc. In these cases, value should be the value of the option selected, not the visible text.
Parameters:
locator an element locator
value the value to type
Usage:
Selenium.type(“locator”, “value”);
Selenium.type(“id=123”, “selenium”);
For sample script, please go through the script TextBox.java
To click a link on web page, please use the below command.
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
Usage:
Selenium.click(“locator”);
Selenium.click(“id=123”);
For sample script, please go through the script Link.java
To click a Button on web page, please use the below command.
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
Usage:
Selenium.click(“locator”);
Selenium.click(“id=123”);
For sample script, please go through the script Button.java
Checkbox
To check a Check box on web page, please use the below command.
Check a toggle-button (checkbox/radio)
Parameters:
locator an element locator
Usage:
Selenium.click(“locator”); or Selenium.check(“locator”);
Selenium.click(“id=123”); or Selenium.check(“id=123”);
For sample script, please go through the script Checkbox.java
Radio Button
To Select a radio button on web page, please use the below command.
Check a toggle-button (checkbox/radio)
Parameters:
locator an element locator
Usage:
Selenium.click(“locator”); or Selenium.check(“locator”);
Selenium.click(“id=123”); or Selenium.check(“id=123”);
For sample script, please go through the script radiobutton.java
Checkout Selenium Interview Questions
Select value
To select a value from Dropdown, we need to use the below command.
Select an option from a drop-down using an option locator.
Option locators provide different ways of specifying options of an HTML Select element (e.g. for selecting a specific option, or for asserting that the selected option satisfies a specification). There are several forms of Select Option Locator.
• label=labelPattern: matches options based on their labels, i.e. the visible text. (This is the default.)
o label=regexp:^[Oo]ther
Usage:
selenium.select(sselectLocator, optionLocator);
selenium.select(“id=abc”,”label=shopping”);
For sample script, please go through the script Dropdown.java
Display all Values
In order to display all values of DD, you need to store them in an array.
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
Usage:
selenium.getSelectOptions(selectLocator);
Store those values in an array.
String a[]=selenium.getSelectOptions(“name=sel”)
For sample script, please go through the script Dropdown1.java
Related Page: Synchronisation Commands - SELENIUM
If you want to select values from dropdown step by step you have to use for loop
String b[]=selenium.getSelectOptions(selectLocator);
for (int i = 0; i < b.length; i++)
{
System.out.println(b[i]);
}
For sample script, please go through the script Dropdown2.java
To write results to Notepad you have to use some Java code.
For sample script, please go through the script Dropdown3.java
To capture the screenshot we have to sue below command
Captures a PNG screenshot to the specified file.
Parameters:
filename the absolute path to the file to be written, e.g. “c:blahscreenshot.png”
Usage:
selenium.captureScreenshot(filename);
selenium.captureScreenshot
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);
How to use Java script in selenium:
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
To read data from Excel sheet we need to use some java code..
FileInputStream
Workbook
Sheet
Most testing frameworks have the concept of grouping a set of text fixtures into a ’suite’ so that you can execute a number of related tests together.
Creating test suites with Selenium doesn’t appear to be obvious at first. Here’s a simple guide on how to create and execute suites of functional tests:
Here i am using Junit scripts and Test suite()
First create two scripts….Ex:Google1 and Google2 Junit test cases
Here is the test suite….TestSuite1.java
--------------------------------------------------------------------------------------------------------------------
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
public class TestSuite1 extends TestCase {
public static Test suite()
{
TestSuite nag = new TestSuite();
nag.addTestSuite( Google1.class);
nag.addTestSuite( Google2.class);
return nag;
}
public static void main(String arg[])
{
TestRunner.run(suite());
}
}
-----------------------------------------------------------------------------------------------------------
Run the TestSuite1.java script …it will automatically run the Google1 and Google2…. Even though first test case is failed it will run the second one….we can change the order of the test cases…
After running test scripts in eclipse it would be good if we generate reports. Junit is one the most popular unit testing framework around.
Below are the steps to generate reports:
1. Right click on project and click on “Export” option
2. Expand “General” node and select “Ant Buildfiles”, click on “Next” button
3. Select the project (let all default options), Junit output Directory … F:AutomationFirstjunit click on “Finish” button build.xml file should be created under project folder
2. Now we need to add junit jar and Tools.jar file in Ant’s “Global Entires”
3. Select “Window” menu, click on “Preferences”
4. In “Preferences” window, expand “Ant” root node, click on “Runtime
5. Click on “Classpath” tab, expand “Global Entires”
6. Click on “Add External JARs” button
7. Navigate to your “eclipse” directory (where eclipse is installed). Select “plugins - >org.junit_3.8.2.v3_8_2_v20100427-1100” directory. Select junit.jar file. and add Tools.jar which is
located under java/JDK/Lib
8. Now right click on build.xml file and select Run As -> Ant Build..
9. In “Edit Configuration and launch” window, select “build”, “tootltip (the script we want to run)” and “junitreprot” checkboxes and click on “Run” button
10. All your test scripts will run and finally report will be generated under the same project directory under “junit” folder. To view JUint folder please refresh
11. Click on “index.html”, you will see the report
Tool tip
Selenium.getAttaribute(“locator@foo”);
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.
We face some of the below issues while automating HTTPS sites..
HTTPS sites will show some security notifications below are some:
Firefox:
Screenshot_1189
IE:
Screenshot_1190
To avoid these type of problems we need use some commands..
To over come this issue we need to add certification
1. Need to add remote control configuration RemoteControlConfiguration rcc = new RemoteControlConfiguration ();
2. Set TrustAllSSLCertificates flag to TRUE rcc.setTrustAllSSLCertificates(true);
3. Pass the rcc Instance to Selenium Server SeleniumServer server = new SeleniumServer(rcc);
If the above steps also doesnt work you need to install cyber villian certificate to automate HTTPS sites (Especially for IE)
1. Extract selenium-server.jar
2. In selenium server folder Open sslSupport folder
3. It contains cybervillainsCA.cer please install that certificate
Now we can run HTTPS site without any certification errors.
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 | Dec 21 to Jan 05 | View Details |
Selenium Training | Dec 24 to Jan 08 | View Details |
Selenium Training | Dec 28 to Jan 12 | View Details |
Selenium Training | Dec 31 to Jan 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 .