Posts

About Ternary operator

Consider this scenario... Let there be a two links which are displayed dynamically at run time. In such cases, we can use ternaray operator. Below is the sample code. In the below code at run time there might present either More details or less details and user need to click WebElement link3 = (driver.findElement(By.linkText("More Details").getText() != null)?driver.findElement(By.linkText("More Details"):driver.findElement(By.linkText("Less Details"); link3.click(); Is same as below WebElement link3 = if driver.findElement(By.linkText("More Details").getText()!=null { driver.findElement(By.linkText("More Details"); link3.click(); } else{ driver.findElement(By.linkText("Less Details") link3.click(); } Please refer to this Wikipedia site for more details http://en.wikipedia.org/wiki/%3F:

Creating a property file and loading data into class file in java

Create a text file and save it with some name example data.txt. And the text file contains the data name = google In java class file create a method called property with an argument of type String Code: public class ABC{ public static String configFilePath = "C:/data.txt"; public String propert(String paramName){ String url=""; try { Properties prop = new Properties(); FileInputStream  fis = new FileInputStream(configFilePath); prop.load(fis); url = prop.getProperty(paramName); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return url; } } The above method can be used in any class. for example: public class xyz { static ABC pro = new ABC(); public static void name(){ System.out.println(":::::::::: name" +pro.propert("name")); } Also...

How to create a fire fox profile and invoke that in webdriver

Creating firefox profile: In run type "firefox.exe -P" click on create profile. Follow the wizard which helps in creating the new profile. Copy the address of new profile which is created. Invoking the profile when running the test script Create a public static method called pfirefox. Use this line in that method "FirefoxProfile pf = new FirefoxProfile(new File("Location of the newly created profile address."));  Note: For example if the newly created address is like this "C:\Mozilla\Firefox\Profiles\axgx9a31.profile". Then the address should be changed to "C:\\Mozilla\\Firefox\\Profiles\\axgx9a31.profile". public class xyz{ public static FirefoxProfile pf(){ FirefoxProfile pf = new FirefoxProfile(new File("C:\\Mozilla\\Firefox\\Profiles\\axgx9a31.profile")); System.out.println("::::::::: New profile"); return pf; } } When defining driver  WebDriver driver = new FirefoxDriver(cla...

Regular expression for retrieving a number from a String

Below is the sample code.          String X = " Your payment request has been accepted for processing.                            Your confirmation number for this payment request is : 006993                            Please allow up to three business days for this payment to be  " final Pattern p = Pattern.compile("(\\s\\d+)"); final Matcher m = p.matcher(x); while(m.find()){ String str1 = m.group().trim(); System.out.println("PPPPPPPPPPPPPPPPP "+str1);          } In the above code, we are using 1. class Pattern 2. In class Patter using method compile 3. In the method, use the regular expression "\\s\\d+" (here \\s represents space and \\d represents digits and using + it will retrieve the digit till it ends. If + sign is not used only one digit is displayed). ...

Tips -1

1 . How to set the dimensions for the browser at run time?        syntax:  driver.manage().window().setSize(new Dimension(X,Y));         X= Width, Y = Height

what is WebDriver wait?

when running test suite if need to wait for an element the webDriver has to wait till the element appears, this can be done either by defining static time out in such cases though the element is displayed still webDriver has to wait till the time out occurs. An implementation of the  Wait  interface that may have its timeout and polling interval configured on the fly. But Using WebDriver wait interface, WebDriver will wait for that particular amount of X duration in such a way that it will check for every Y duration(Both X,Y are configurable) whether the element is displayed or not, it is like dynamic wait. Syntax: // Waiting 30 seconds for an element to be present on the page, checking // for its presence once every 5 seconds. FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class); The below code can be used in a method, ...

How to select a value from drop down in webdriver

Consider the scenario where in a drop down there are 20 items and need to select an item via webdriver. By using the following code this can be solved 1. WebElement select = driver.findElement(By.id("XYZ")); 2. List<WebElement> alloptions = select.findElements(By.tagName("option")); 3. for(WebElement option: alloptions){ 4. System.out.println(String.format("value is:%s", option.getText())); 5. if(option.getText().equalsIgnoreCase("ABC")){ 6. option.click(); } } In the above code       line 1: drop down is identified either                by id, by tag name, by name       line 2: reading list of all items present in the drop down        line 3: using for loop list items from line 2 passed into webElement       line 4: used to print the content in console       line 5: using If condition if it is s...