Category Archives: Selenium

Selenium Basics

In the last post we have seen the steps to setup the project in eclipse. Today lets see the further more in selenium [Java] with writing a test case for ordering a product in ebay.

1. Create a class called EbayOrder.java

2. Load the driver based on your requirement either IE & Firefox, Here i am loading the Firefox driver.

// To load driver for firefox browser.
WebDriver driver = new FirefoxDriver();

3. Add implicit wait, to wait until the page loads or a particular milliseconds.

// Implicit wait for 10 seconds on each step to load the page.
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

4. To open a browser and goto ebay.co.in use driver.get() method,

// To navigate to the particular URL.
driver.get("http://www.ebay.in/");

5. To find the element based on the Xpath using firepath plugin and type the product name,

// To find the element by xpath and type something.
driver.findElement(By.xpath(".//*[@id='gh-ac']")).sendKeys("Sony xperia Z1");

6. To Click the search button using id, Which is submit button,

// To click the Search Button based on Id.
driver.findElement(By.id("gh-btn")).submit();

6. To wait until the particular element or page loads we could use the Explicit wait with conditions, Here i am using this for wait until the product list load,

// To wait until the product list to be clickable.
WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*@id='ResultSetItems']/table[1]/tbody/tr/td[1]/div/div/div/a/img")));

7. To select the product based on xpath,

// To Select the Product.
driver.findElement(By.xpath(".//*[@id='ResultSetItems']/table[1]/tbody/tr/td[1]/div/div/div/a/img")).click();

8. To Click the BuyNow button,

// To Click BuyNow.
driver.findElement(By.xpath(".//*[@id='but_v4-28binLnk']")).click();

9. Like above You Could continue the test case like login, payment and so on.,

10. To exit at the end,

driver.close();

EbayOrder.java

package com.mahesh.selenium.Main;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class EbayOrder {

public static void main(String[] args) {
 // To load driver for firefox browser.
 WebDriver driver = new FirefoxDriver();

// Implicit wait for 10 seconds on each step to load the page.
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

// To navigate to the particular URL.
 driver.get("http://www.ebay.in/");

// To find the element by xpath and type something.
 driver.findElement(By.xpath(".//*[@id='gh-ac']")).sendKeys(
 "Sony xperia Z1");

// To click the Search Button based on Id.
 driver.findElement(By.id("gh-btn")).submit();

// To wait until the product list to be clickable.
 //WebDriverWait wait = new WebDriverWait(driver, 100);
 //wait.until(ExpectedConditions.elementToBeClickable(By
 //.xpath(".//*[@id='ResultSetItems']/table[1]/tbody/tr/td[1]/div/div/div/a/img")));

// To Select the Product.
 driver.findElement(
 By.xpath(".//*[@id='ResultSetItems']/table[1]/tbody/tr/td[1]/div/div/div/a/img"))
 .click();

// To BuyNow.
 driver.findElement(By.xpath(".//*[@id='but_v4-28binLnk']")).click();

// Like above You Could continue the test case like login & payment.

//driver.close();
 }

}

Happy Learning!!!

Advertisements

What is firebug & fire path?

Firebug is a web development tool that facilitates the debugging, editing, and monitoring of any website’s CSS, HTML, DOM, XHR, and JavaScript;

FirePath is a Firebug extension that adds a development tool to edit, inspect and generate XPath expressions.

Using this firebug & firepath we could able to find the html element attributes.

How to install Firebug & Firepath?

  1. Goto https://getfirebug.com/ and click on the installfirebug button and follow the steps and install firebug as an add-on in the Mozilla Firefox browser.
  2. Once installed firebug, You would see the addon like this,firebug1firebug2
  1. After you have installed firebug you could be able to install firepath, go to the url: https://addons.mozilla.org/en-US/firefox/addon/firepath/
  2. Click on the button ‘Add to Firefox’ and follow the steps to install firepath on firebug.
  3. Once installed firepath, You would get like this,firepath

Selenium Web Driver [Java] Introduction

What is Selenium?

Selenium is an automate testing framework for web applications. Selenium is composed of multiple software tools. Each has a specific role. Here let’s see about selenium web driver.

Selenium Web Driver

Selenium Web Driver accepts commands and sends them to a browser. This is implemented through a browser-specific browser driver, which sends commands to a browser, and retrieves results. Let’s see how to set up and do a simple automate test using java language.

Prerequisites

Project Step-up

  1. Create a java project in Eclipse like below, File->New->Java ProjectCreating Java Project
  2. Then create a Class named SeleniumMain (As your wish) like below,Create Class
  3. Now add the Selenium jar to build path, which is downloaded before.

    Adding Jars into built path

  4. Now you could start write the code for testing the Application, Here let’s see how to open a flipkart website in Mozila firefox browser,Firefox Driver
  5. While running the SeleniumMain.java class, You would get the output as below,Firefox O/P
  6. If u want to use Internet explorer, the you should load the IEDriverServer.exe, which available in the below path,http://code.google.com/p/selenium/downloads/listIE Driver
    SeleniumMain.Java

    <br>package com.mahesh.selenium.Main;import java.io.File;  <p>import org.openqa.selenium.WebDriver;<br>import org.openqa.selenium.ie.InternetExplorerDriver;</p> <p>public class SeleniumMain {</p> <p>/**<br>* @param args<br>*/<br>public static void main(String[] args) {</p> <p>// Firfox driver.<br>//WebDriver driver = new FirefoxDriver();</p> <p>//IE Driver<br>File file = new File("C:/Jars/IEDriverServer.exe");<br>System.setProperty("webdriver.ie.driver", file.getAbsolutePath());<br>WebDriver driver = new InternetExplorerDriver();</p> <p>driver.get("http://www.flipkart.com");<br>}<br>}<br>

    IE ResultHappy Learning!!!