Arrays is a data structure that is using in many programming languages to implementing and manipulating sets of similar type data. In Selenium IDE, Array can be used to enter a sets of data one by one in an input field or clicking on different link or button to repeat the same task.
Here we will explain how it can be performed.
Step 1 : We have a sample task on Selenium page. We want to visit different tabs on selenium page, Tabs are : Project, Download, documentation, Support, About.
I want to make a data structure where I store the tab name in the array and IDE script will read the tab name from Array and execute the click command according to it. Note that we will also implement while loop here.
Array will be,
tab[0]=”Projects”
tab[1]=”Download”
tab[2]=”Documentation”
tab[3]=”Support”
tab[4]=”About”
Step 2 : In order to traversing the Array we need a loop command. In that case we will use while loop but “while” command is not a built in command in selenium IDE. We have to download the user extensions from the following location.
https://docs.google.com/file/d/0B6vnknygMB3ISHBYODA3UDUtS0U/edit?usp=sharing
After downloading the user-extension.js file we need to attach it to Selenium IDE.
For this,
- Go to Options>Options…
- Options window will be displayed and go to General tab.
- Locate the user-extension.js file and click on OK button.
Step 3 : Write the following Selenium command and that will be,
Here we use the storeEval and getEval command.
storeEval : Execute the Java script command and return the value and assign the value Value column.
getEval : Execute the Java script command but does not do any return.
Line 1: Open the application with the base URL : http://www.seleniumhq.org/
Line 2 : Define the array
Command | Target | Value |
storeEval | new Array("Projects", "Download", "Documentation","Support","About") | tabNameArray |
Line 3 : Define a variable.
Command | Target | Value |
getEval | tabItems=0; |
Line 4 : Write while command and condition.
Command | Target | Value |
while | tabItems<storedVars['tabNameArray'].length |
Note that we use storedVars['tabNameArray'] here to return the array reference.
Line 5 : Store tabItems to TabN
Command | Target | Value |
storeEval | tabItems | tabN |
Line 6 : Return the tab name from tabNameArray and store it to tabName.
Command | Target | Value |
storeEval | storedVars['tabNameArray'][storedVars['tabN']] | tabName |
So, tabNameArray[0] =”Projects”
tabNameArray[1] =”Download”
and so on….
Line 7 : Print the tab name.
Command | Target | Value |
echo | javascript{storedVars['tabName']} |
Line 8 : Click on the tab by Link name.
Command | Target | Value |
clickAndWait | link=${tabName} |
Line 9 : Increase the value for tabItems to 1.
Command | Target | Value |
getEval | tabItems++ |
Line 10 : End of the while loop.
Command | Target | Value |
endWhile |
Step 4 : Run the script.
In this way we can also store user data to an array and use those array data as input of the application. So driven testing can also be performed in this way. Thanks…
Happy Selenium IDE scripting with Array…
Wednesday, September 17, 2014
How to run Selenium IDE script in different browser
But in order to do cross browser testing we need to run the script on other browsers like chrome, Internet Explorer etc.
Selenium IDE has given a feature (that is still experimental stage) where we can run our recorded IDE scripts through Chrome, Internet Explorer etc.
Step 1 : Download Selenium Standalone server and Browser driver server
Download the selenium Standalone server from the location (http://www.seleniumhq.org/download/).
Download the Browser Driver from the following location.
- Internet Explorer(IE) driver :
- Go to the location (http://www.seleniumhq.org/download/).
- Note that there are two different version for 32 bit and 64 bit windows. Click on the appropriate link (32 bit windows IE or 64 bit windows IE) according to your windows OS version (32 or 64 bit).
- Click on the above selected link and download the zip file. Unzip the file and copy the EXE file to a single location where Selenium Standalone server is located.
- Chrome Driver :
- Go to this link (https://code.google.com/p/selenium/wiki/ChromeDriver)
- At first, click on “Download chromedriver” link and it moves to another page of all chrome driver release (http://chromedriver.storage.googleapis.com/index.html).
Step 2 : Configure Selenium IDE
- Click to download the latest chrome driver. Here I have downloaded version 2.10. But it might be more latest version in future release. Clicking on this link will go to the following page.
- Click on the above selected link and download the zip file. Unzip the file and copy the EXE file to a single location where Selenium Standalone server and IE driver are located.
- Open Selenium IDE
- Go to Options
- Click on Options menu
- Selenium IDE options window is displayed and click on Webdriver tab.
- Now check Enable Webdriver Playback options
- Enter the full name of the browser you want to run your script in the text box below the checkbox. For example, in case of IE, we need to type “internet explorer”. In case of other browser, we can enter chrome, android, firefox, htmlunit, iPhone, iPad, opera.
- Click ok button
- Now restart the Selenium IDE. A restart is required to enable and disable webdriver. By default this option is disabled at selenium IDE.
- Before running this server ensure that java runtime environment is already installed on that machine as this is required to run JAR file. If not then download it from specific location.
- Open the command prompt by using admin privilege (Run as administrator).
- Go to the location where the Selenium Standalone server JAR file located. In my case, this is C:\Run location.
- If you want to run your script on Internet explorer then type following command where both Selenium Standalone server and IE driver will start running.
java -jar selenium-server-standalone-2.42.2.jar -Dwebdriver.ie.driver=IEDriverServer.exe
- In case of chrome driver the command will be
All of the cases the command syntax will bejava -jar selenium-server-standalone-2.42.2.jar -Dwebdriver.chrome.driver=chromedriver.exe
java -jar seleniumStandaloneServerFileName.jar -Dwebdriver.BrowserName.driver=.BrowserDriverServerFileName.exe
Note that every time we run the selenium standalone server with the browser driver server.
Step 4 : Run Selenium IDE scripts
- Open Selenium IDE.
- Load the recorded script file.
- Run the script.
- Note that the browser will be pop up and start executing the script.
Please comment me on any error you found to execute these steps.
Happy Cross Browser Testing!!!
Thursday, August 14, 2014
Easy way to determine the better XPath for Selenium Webdriver scripts
But if we use the XPath that is nearly better and developed by some of the Selenium tools like Selenium IDE, is more better than any other tools and nearly well known for Selenium webdriver API.
Here I will show you how to determine XPath for Selenium Webdriver scripts to locate element in the web page by using our well known Selenium IDE tools.
Installing the IDE tools:
You may know How to install Selenium IDE Plug in on your Firefox Browser. If not then click here.
Determining the XPath:
Step 1: Open Firefox browser.
Step 2: Click on the Selenium IDE icon on Plug in tool bar.
Step 3 : Selenium IDE window is displayed. Now click the red color record button to make the record on.
Step 4 : Now minimize the IDE window and enter the application URL : https://www.google.com.bd/ at the Firefox browser
Step 5 : Enter the text “Selenium IDE” at Google search text box and click on Search button.
Step 6 : Now Restore the Selenium IDE window and click red color button again to Stop the recording. You can play the script by clicking on green color play button.
Step 7 : Click on a Step. Command, Target and value is displayed at the bottom of the window.
Step 8 : Here we see “id” as a locator in target combo box. If we click on the combo box we see a drop down list of other locators like Name, CSS, XPath etc.
Step 9 : There are three types of XPath here – Attribute, relative and Position. Most of the cases position is working fine. We select the XPath : Position at the target combo box. Now click on “Find” button to verify that it can locate the actual element.
Step 10 : Copy the XPath from Target box and paste to the selenium Webdriver code as Locator.
C# :
Java :driver.FindElement(By.XPath("//td[2]/div/input")).SendKeys("Selenium IDE");
driver.findElement(By.xpath("//td[2]/div/input")).sendKeys("Selenium IDE");
Note that we will only use XPath if and only if id, Name or CSS are not available as locator.
Thanks!!!
Monday, October 17, 2011
How to run Selenium WebDriver code with JUnit
In my previous post I have shown how to run Selenium WebDriver Code with NUnit.
Now I will show you how Selenium WebDriver code generated from selenium IDE can be run at JUnit4 Framework.
Selenium IDE has a functionality to convert its recorded script to Selenium WebDriver and Java in JUnit4 Framework.
So If we know how to record Selenium IDE (see the post : http://bit.ly/pa1TG5) then we can easily convert it to selenium WebDriver code.
Before starting we just define a Test case.
Test case :
- Open Selenium Website (http://seleniumhq.org/) in Firefox browser.
- Click Project tab
- Click Selenium IDE link
- Verify that Selenium IDE Text is present
Step 1: Record the test case in Selenium IDE
- Open Firefox Browser
- Open Selenium IDE window by Clicking Tools->Selenium IDE
- Make Red colored Record button on.
- Enter the URL : http://seleniumhq.org/ at the address bar and click Search button
- Click Project tab
- Click Selenium IDE link
- Verify that Selenium IDE Text is present
The recorded script will be like the following figure
Now make the record button off and Click Run button to run the script.
Step 2: Convert the Selenium IDE recorded script to JUnit 4 (WebDriver) code
- Go to Selenium IDE window
- Click Options menu
- Click Format –> JUnit 4 (WebDriver)
Sometimes in Options –> Format menu, JUnit 4 (WebDriver) options may not be displayed. The menu options like:
In that case, there is some change needed in selenium IDE option window. Please follow the steps:
- Click Options menu in Selenium IDE window
- Click “Options…” and Selenium IDE options window is displayed.
Click Enable experimental features check box and click OK button. Now go to Format options and driver list.
- A message dialog is displayed after selecting the appropriate format like (JUnit 4 (WebDriver))
- Click OK button
- Selenium IDE window shows the selenium WebDriver Code in Java format with unit testing Framework JUnit 4 is implemented.
Step 2: Run the Code in Eclipse IDE
- Open Eclipse IDE
- Create a New Java project by Clicking the options File –>New –>Java Project and New Java Project Window is displayed
- Enter Project name and select Project execution environment as JavaSE-1.6
- Click Next button
- Click Finish button
- New Project is created
- Add a New Java Class to the project
- Now Copy the whole text from Selenium IDE and paste into New Java class file. It is like Copy the whole text like CTRL+A to select the whole text and CTRL+C to copy.
- There are some Reference error show at editor.
- Right click on the project name and click Properties and Properties window is displayed
- Click Java Build path and click Libraries Tab
- Click Add Library button
- Add Library window is displayed
- Select JUnit and click Next
- Select JUnit4 as JUnit Library and click Finish button
- JUnit4 library is added and click OK button of properties window
- Now go to selenium download page ( http://seleniumhq.org/download/ ) and download the latest version of Selenium Client Driver for Java. Here version 2.8.0 is downloaded.
- Now unzip downloaded zip file and rename the folder as “Selenium” and placed to the Current project folder
- Right click on the Project name in Eclipse and click Properties and properties window is displayed
- Click Java Build Path
- Go to Libraries tab and click Add External JARs
- Go to Selenium folder and select the jar file named “selenium-java-2.8.0.jar” and click Open. Selenium webdriver jar file is added
- Click OK button of Properties window
- Remove the package name “package com.example.tests;”
- Rename the Class name Untitled to Java Class file name
- Add the following code at the testUntitled() method
Assert.assertEquals("Text Found", "Selenium IDE", driver.findElement(By.xpath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).toString());
- The method will be
public void testUntitled() throws Exception {driver.get("http://seleniumhq.org/");driver.findElement(By.linkText("Projects")).click();driver.findElement(By.linkText("Selenium IDE")).click();Assert.assertEquals("Text Found", "Selenium IDE", driver.findElement(By.xpath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).toString());}
- The whole codes are
1:2:3: import java.util.regex.Pattern;4: import java.util.concurrent.TimeUnit;5:6: import junit.framework.Assert;7: import junit.framework.AssertionFailedError;8:9: import org.junit.*;10: import static org.junit.Assert.*;11: import static org.hamcrest.CoreMatchers.*;12: import org.openqa.selenium.*;13: import org.openqa.selenium.firefox.FirefoxDriver;14: import org.openqa.selenium.support.ui.Select;15:16: public class testSel {17: private WebDriver driver;18: private String baseUrl="";19: private StringBuffer verificationErrors = new StringBuffer();20: @Before21: public void setUp() throws Exception {22: driver = new FirefoxDriver();23: driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);24: }25:26: @Test27: public void testUntitled() throws Exception {28: driver.get("http://seleniumhq.org/");29: driver.findElement(By.linkText("Projects")).click();30: driver.findElement(By.linkText("Selenium IDE")).click();31: Assert.assertEquals("Text Found", "Selenium IDE", driver.findElement(By.xpath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).toString());32: }33: @After34: public void tearDown() throws Exception {35: driver.quit();36: String verificationErrorString = verificationErrors.toString();37: if (!"".equals(verificationErrorString)) {38: fail(verificationErrorString);39: }40: }41:42: private boolean isElementPresent(By by) {43: try {44: driver.findElement(by);45: return true;46: } catch (NoSuchElementException e) {47: return false;48: }49: }50: }51:
- Now Run project by click Run button from JUnit4
- Firefox Browser is opened and Script runs and Test is successful.
That’s all. I have described here all the steps in a very easy and details way. If any thing could not be understand, please comment me, I must answer. Please comment me about the whole blog.
Tuesday, September 20, 2011
How to run Selenium Webdriver code with Visual Studio Test Framework
Previous Post, I have shown you how to run selenium WebDriver code in Visual Studio with NUnit Framework.
Now I will show you how Selenium WebDriver code generated from selenium IDE can be run at Visual studio Test Framework.
Selenium IDE has a functionality to convert its recorded script to Selenium WebDriver and C# in NUnit Framework. Here we copy some portion of code and paste it to Visual studio test framework
So If we know how to record Selenium IDE (see the post : http://bit.ly/pa1TG5) then we can easily convert it to selenium WebDriver code.
Before starting we just define a Test case.
Test case :
- Open Selenium Website (http://seleniumhq.org/) in Firefox browser.
- Click Project tab
- Click Selenium IDE link
- Verify that Selenium IDE Text is present
Step 1: Record the test case in Selenium IDE
- Open Firefox Browser
- Open Selenium IDE window by Clicking Tools->Selenium IDE
- Make Red colored Record button on.
- Enter the URL : http://seleniumhq.org/ at the address bar and click Search button
- Click Project tab
- Click Selenium IDE link
- Verify that Selenium IDE Text is present
The recorded script will be like the following figure
Now make the record button off and Click Run button to run the script.
Step 2: Convert the Selenium IDE recorded script to C# WebDriver code
- Go to Selenium IDE window
- Click Options menu
- Click Format –> C#(WebDriver).
Sometimes in Options –> Format menu, C#(WebDriver) options may not be displayed. The menu options like:
In that case, there is some change needed in selenium IDE option window. Please follow the steps:
- Click Options menu in Selenium IDE window
- Click “Options…” and Selenium IDE options window is displayed.
Click Enable experimental features check box and click OK button. Now go to Format options and driver list.
- A message dialog is displayed after selecting the appropriate format like (C#(WebDriver))
- Click OK button
- Selenium IDE window shows the selenium WebDriver Code in C# format with unit testing Framework NUnit is implemented.
Step 2: Run the Code in visual Studio 2010 Test Framework
- Open Visual Studio 2010 IDE
- Create a Test Project by Clicking Test->New Test. A Test project is created with a default class name UnitTest1.cs
- Double click on UnitTest1.cs on Solution Explorer to open the class file
- Now go to Selenium IDE code editor and find the method name “TheUntitledTest()”
- Copy the entire code of this method and paste to “TestMethod1()” method of VS Test project and the method “TestMethod1()” looks like the following
{
[TestMethod]
public void TestMethod1()
{
driver.Navigate().GoToUrl(baseURL);
driver.FindElement(By.LinkText("Projects")).Click();
driver.FindElement(By.LinkText("Selenium IDE")).Click();
// ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
}
}
- Now Add two selenium namespace
- using OpenQA.Selenium;
- using OpenQA.Selenium.Firefox;
- Now go to selenium download page ( http://seleniumhq.org/download/ ) and download the latest version of Selenium Client Driver with C#. Here version 2.6.0 is downloaded.
- Now unzip downloaded zip file and rename the folder as “Selenium”
- Go to Solution explorer of Visual Studio IDE
- Right click Reference and click Add Reference option. Add Reference window is displayed
- Click Browse tab and go to Selenium folder. Inspect that there are 2 sub folder – net35 and net40. net35 folder is for .Net framework 3.5 project and net40 for .Net framework 4.0 project. In this project .Net framework 4.0 is selected so we go to net40 folder
- Select all the dlls and click OK button and .dll files are added as reference
- Now all the references are added
- Go to “public void TestMethod1()” method
- Create Firefox Driver object named “driver” and the code will be
- Add a string variable for base URL named “baseURL”. Code looks like
- Add the following code at the TestMethod1() method for verification
- For closing the browser window add the code at last line
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace TestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
FirefoxDriver driver = new FirefoxDriver();
string baseURL = "http://seleniumhq.org/";
driver.Navigate().GoToUrl(baseURL);
driver.FindElement(By.LinkText("Projects")).Click();
driver.FindElement(By.LinkText("Selenium IDE")).Click();
Assert.AreEqual(driver.FindElement(By.XPath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).Text, "Selenium IDE");
driver.Close();
}
}
}
- Build the solution by right clicking on the solution name and Click build solution
- Inspect that no Error message is displayed and build is succeeded.
- Click Run button
- Firefox Browser is opened and Script runs and Test is successful.
That’s all. I have described here all the steps in a very easy and details way. If any thing could not understand, please comment me, I must answer.
In my next Selenium blog, I will show how we can run our selenium WebDriver code in Java with JUnit4 Framework.
Monday, September 19, 2011
How to run Selenium WebDriver code with NUnit
In the last post I have shown you how to record and play back with Selenium IDE.
Now I will show you how Selenium WebDriver code generated from selenium IDE can be run at visual studio 2010 with NUnit Framework.
Selenium IDE has a functionality to convert its recorded script to Selenium WebDriver and C# in NUnit Framework.
So If we know how to record Selenium IDE (see the post : http://bit.ly/pa1TG5) then we can easily convert it to selenium WebDriver code.
Before starting we just define a Test case.
Test case :
- Open Selenium Website (http://seleniumhq.org/) in Firefox browser.
- Click Project tab
- Click Selenium IDE link
- Verify that Selenium IDE Text is present
Step 1: Record the test case in Selenium IDE
- Open Firefox Browser
- Open Selenium IDE window by Clicking Tools->Selenium IDE
- Make Red colored Record button on.
- Enter the URL : http://seleniumhq.org/ at the address bar and click Search button
- Click Project tab
- Click Selenium IDE link
- Verify that Selenium IDE Text is present
The recorded script will be like the following figure
Now make the record button off and Click Run button to run the script.
Step 2: Convert the Selenium IDE recorded script to C# WebDriver code
- Go to Selenium IDE window
- Click Options menu
- Click Format –> C#(WebDriver).
Sometimes in Options –> Format menu, C#(WebDriver) options may not be displayed. The menu options like:
In that case, there is some change needed in selenium IDE option window. Please follow the steps:
- Click Options menu in Selenium IDE window
- Click “Options…” and Selenium IDE options window is displayed.
Click Enable experimental features check box and click OK button. Now go to Format options and driver list.
- A message dialog is displayed after selecting the appropriate format like (C#(WebDriver))
- Click OK button
- Selenium IDE window shows the selenium WebDriver Code in C# format with unit testing Framework NUnit is implemented.
Step 2: Run the Code in visual Studio 2010
- Open Visual Studio 2010 IDE
- Create a New Class Library project by Clicking the options File –>New –>Project and New Project Window is displayed
- Select Visual C# from the template and project type Class Library
- Enter Project name, Location and Solution Name and Select .Net Framework 4.0
- Click OK button
- New Project is created
- Now Copy the whole text from Selenium IDE and paste into Class Library project editor. It is like Copy the whole text like CTRL+A to select the whole text and CTRL+C to copy. Then remove all sample code from VS editor and Paste CTRL+V to all code.
- There are some Reference error show at editor. It could not find Open QA namespace. For this we need some .dll file from Selenium client driver to add as a reference.
- Now go to selenium download page ( http://seleniumhq.org/download/ ) and download the latest version of Selenium Client Driver with C#. Here version 2.6.0 is downloaded.
- Now unzip downloaded zip file and rename the folder as “Selenium”
- Go to Solution explorer of IDE
- Right click Reference and click Add Reference option. Add Reference window is displayed
- Click Browse tab and go to Selenium folder. Inspect that there are 2 sub folder there – net35 and net40. net35 folder is for .Net framework 3.5 project and net40 for .Net framework 4.0 project. If the class library project is created as ,Net framework 3.5 then we select the .dll file of net35 folder. In this project .Net framework 4.0 is selected so we go to net40 folder
- Select all the dlls and click OK button and .dll files are added as reference
- Now NUnit dll’s are needed for NUnit framework
- Now download the latest NUnit form the site (http://www.nunit.org/index.php?p=download)
- Download the latest NUnit with windows verison (2.5.10) and Install
- Now right click on Reference and click Add Reference and go to NUnit folder where it is installed like (C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\framework)
- Select (nunit.framework.dll) file and click OK
- Now all the references are added
- Add the following code at the TheUntitledTest() method
- TheUntitledTest() method will be
- public void TheUntitledTest()
- {
- driver.Navigate().GoToUrl(baseURL);
- driver.FindElement(By.LinkText("Projects")).Click();
- driver.FindElement(By.LinkText("Selenium IDE")).Click();
- Assert.AreEqual(driver.FindElement(By.XPath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).Text, "Selenium IDE");
- // ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
- }
- Class1.cs code (whole WebDriver code) will be
- using System;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using NUnit.Framework;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Firefox;
- namespace SeleniumTests
- {
- [TestFixture]
- public class Untitled
- {
- private IWebDriver driver;
- private StringBuilder verificationErrors;
- private string baseURL;
- [SetUp]
- public void SetupTest()
- {
- driver = new FirefoxDriver();
- baseURL = "http://seleniumhq.org/";
- verificationErrors = new StringBuilder();
- }
- [TearDown]
- public void TeardownTest()
- {
- try
- {
- driver.Quit();
- }
- catch (Exception)
- {
- // Ignore errors if unable to close the browser
- }
- Assert.AreEqual("", verificationErrors.ToString());
- }
- [Test]
- public void TheUntitledTest()
- {
- driver.Navigate().GoToUrl(baseURL);
- driver.FindElement(By.LinkText("Projects")).Click();
- driver.FindElement(By.LinkText("Selenium IDE")).Click();
- Assert.AreEqual(driver.FindElement(By.XPath("//div[@id='mainContent']/table/tbody/tr/td/p/b")).Text, "Selenium IDE");
- // ERROR: Caught exception [ERROR: Unsupported command [isTextPresent]]
- }
- private bool IsElementPresent(By by)
- {
- try
- {
- driver.FindElement(by);
- return true;
- }
- catch (NoSuchElementException)
- {
- return false;
- }
- }
- }
- }
- Build the solution by right clicking on the solution name and Click build solution
- Inspect that no Error message is displayed and build is succeeded.
- Now open NUnit application editor by going Start – > Program – > NUnit 2.5.10 –> NUnit
- Now go to File – > Open Project
- Select the Class Library dll file from bin/debug folder of Class Library project
- Click Run button
- Firefox Browser is opened and Script runs and Test is successful.
That’s all. I have described here all the steps in a very easy and details way. If any thing could not be understand, please comment me, I must answer. Please comment me about the whole blog.
In my next blog, I will show how we can run our selenium WebDriver code in Visual Studio Test Framework.