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!!!

Leave a comment