What is Selenium?
In lay mans terms, selenium is a program which we can use to automate web page related tasks by programmatically replicating the manual tasks of key presses and mouse clicks.
How to use Selenium in Eclipse?
For Selenium to be used in Eclipse IDE, the IDE should be able to access the classes specified in the Selenium Package. This can be achieved by either adding the JAR file as an external library or adding the maven dependency in pom.xml.
The JAR file and the dependency can be downloaded from :
How to code for testing in Selenium?
To get the Test Script working, the following steps need to be followed:
- Add Selenium to Eclipse IDE.
- Import the required classes (WebDrivers).
- Declare a WebDriver object.
- Fetch the URL.
- Find the Required component.
- Perform the required action (click, enter key-strokes, etc.).
- Close the WebDriver.
Adding Selenium to Eclipse IDE:
Mentioned in “How to use Selenium with Eclipse?”.
Import the Required Classes:
To create a Browser instance, a “WebDriver” object needs to be created. For this “org.openqa.selenium.WebDriver”needs to be imported. Since Selenium uses “Firefox” browser as its default browsing engine, to us any other browser such as “Chrome”, we need to import its supporting class separately (org.openqa.selenium.chrome.ChromeDriver).
While using Chrome, the driver for it, “chromdriver.exe”, needs to be downloaded separately and called in the class where the WebDriver is being declared.
This is done as follows:
Declare a WebDriver Object:
Declaring the WebDriver is done as follows-
driver = new ChromeDriver();
driver = new FirefoxDriver();”
/*Depending on which browser is being used*/
Fetch the URL:
The browser is opened and page is fetched by using the following code:
Once the URL is opened, any required action on its components can be performed.
Perform Required Actions:
- Finding elements and Clicking on them – Elements can be found using their CSS or XPATH.
Css Selector- “driver.findElement(By.cssSelector(“<cssSelector>”)).click();”
XPATH Selector- “driver.findElement(By.xpath(“<xPath>”)).click();”
- Filling data in text field – For this the text field must be found and cleared before new text is added to it.
Clearing Field – “driver.findElement(By.xpath(“<xpath>”)).clear();”
Adding Key-Strokes – “driver.findElement(By.xpath(“<xpath>”)).sendKeys(“AutomatedTest”);”
- Drop-Down Menu – To access the drop down menu, the following method works best.
“Select droplist = new Select(driver.findElement(By.cssSelector(“<cssSelector>”)));
droplist.selectByVisibleText(“<visible text of menu to be selected>”);”
- Waiting for page or component load – At times, the program needs to wait for new page or modal windows to load before continuing. For this the following code is to be used.
“Thread.sleep(<time in miliseconds>);”
- Using the “Enter” key – At times, its easier to use the “Enter” key than to click on “ok” or “submit”.
“driver.findElement(By.xpath(“<xpath of last changed component>”)) .sendKeys(Keys.RETURN);”
The code must always be included in try – catch blocks.
The page should be given time to load.
Css and xPath do not always work for every component in every browser so use whichever one is working.
Best Open Source Business Intelligence Software Helical Insight is Here