Wednesday, April 16, 2014

Automating real time test cases using Selenium Webdriver - 2

Test Scenario: To verify if the search results are within the specified price range.

Test Steps:

 Navigate to http://m.propertyguru.com.my/ 
 Select Min Price to 8000000 
 Select Max Price to 10000000
 Click on Search button
 Verify if the results are within the specified price range.

Note: Results Highlighted in yellow tagged with a star image should not be included on the verification. They are usually found on the first 3 rows and categorized as featured ads. Those are not part of the search results that you need to verify/assert.

Test Case Code Snippet:


package com;

import java.util.concurrent.TimeUnit;


import org.junit.Before;

import org.junit.After;
import org.junit.Test;

/*import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;*/

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class VerifyRangeValues{

private WebDriver driver;
private String baseUrl;
// private int numToVerify;

public static String stripNonDigits(
            final CharSequence input /* inspired by seh's comment */){
    final StringBuilder sb = new StringBuilder(
            input.length() /* also inspired by seh's comment */);
    for(int i = 0; i < input.length(); i++){
        final char c = input.charAt(i);
         if(c > 47 && c < 58){ // Checking the ASCII between 48 to 57 for 0 to 9
         sb.append(c);
        }
    }
    return sb.toString();
}

public void compareNumWithRange(String strNum){
final String result = stripNonDigits(strNum);
final int a = (Integer.valueOf(result)).intValue();
System.out.println("Value extracted from string " + a);
if (a >= 8000000 && a <= 10000000){
System.out.println(a + "  is within the range 8000000 to 10000000 specified");
}
 else {
 System.out.println(a + "  is NOT withing the range 8000000 to 10000000 specified");
   
}

}


@Before
public void setUp() throws Exception{

//Uncomment the folling block and commented imports to run the code in Chrome Driver.

/*System.setProperty("webdriver.chrome.driver","E:\\D-Drive-Data\\Softwares\\Selenium Support Files\\chromedriver_win32\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);*/

driver = new FirefoxDriver();
baseUrl = "http://m.propertyguru.com.my";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("SetUp executed succesfully");

}

@Test
public void testValueRange() throws Exception{
driver.get(baseUrl +"/");


Select minPriceDropdown = new Select(driver.findElement(By.id("minprice-sale")));
minPriceDropdown.selectByVisibleText("RM 800,000");

Select maxPriceDropdown = new Select(driver.findElement(By.id("maxprice-sale")));
maxPriceDropdown.selectByVisibleText("RM 1,000,000");

//driver.findElement(By.id("maxprice-sale")).sendKeys("RM 1,000,000");
//driver.findElement(By.xpath(".//*[@id='maxprice-sale']/option[1000000]"));
driver.findElement(By.id("btnSearch")).submit();

/*compareNumWithRange(
driver.findElement(By.xpath(".//*[@id='listing[0-9]{7}']/table/tbody/tr/td/div[2]/p[1]/strong/span")).getText()); */
for(int i = 4; i <11 font="" i="">
System.out.println(driver.findElement(By.cssSelector("#listings li:nth-child("+i+") strong > span")).getText());
//driver.wait(15000);

compareNumWithRange(
driver.findElement(By.cssSelector("#listings li:nth-child("+i+") strong > span")).getText());


System.out.println(i+ " testValueRange() executed succesfully");
}
}

@After
public void tearDown() throws Exception {
System.out.println("tearDown() executed succesfully");
driver.quit();
System.out.println("Webdriver Quit Successfully.");


}



}


Challenge!

Do you have a better and faster way to do this?

No comments:

Post a Comment