Tag Archives: java

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

How to do Pattern based Search in String and replace with another in Java

In our day today life we have faced some challenges like, to replace the particular pattern (String) by some other string. In this scenario we could use the Pattern matcher with regular expressions. Let’s see how we could achieve it.

Our input string is,

String inputStr = "Every week here and in our newsletter, 143-37-8945 we feature a new developer/blogger from the DZone community to catch up and find out what243-45-3442 he or she is working";

In this above String I want to find the numbers like this 143-37-8945 & 243-45-3442 and mask the last 4 digits.

Now we have to form a regular expression like as below,

(\\d{3}-\\d{2}-\\d{4})

Then we have to match the pattern with input string and find the pattern XXX-XX-XXXX, then have to mask it.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternSearch {
public static void main(String[] args) {

// Input string.
String inputStr = "Every week here and in our newsletter, 143-37-8945 we feature a new developer/blogger from the DZone community to catch up and find out what243-45-3442 he or she is working";

// Regular expression to find SSN.
Pattern pattern = Pattern.compile("(\\d{3}-\\d{2}-\\d{4})");

Matcher matcher = pattern.matcher(inputStr);

StringBuffer result = new StringBuffer();

// Every match of SSN, this loop will execute.
while (matcher.find()) {
System.out.println(matcher.group(1));
matcher.appendReplacement(result, matcher.group(1).substring(0, 7)
+ "XXXX");
}
matcher.appendTail(result);
System.out.println(result);
}
}

Output:

143-37-8945

243-45-3442

Every week here and in our newsletter, 143-37-XXXX we feature a new developer/blogger from the DZone community to catch up and find out what243-45-XXXX he or she is working.

Happy Learning!!!