Posts

To check if Web Element is present

To verify if an element is present in the screen  and based on the condition to proceed, then we can check the size of element and if it is greater than zero then the element exists or else element does not exists Ex: Consider login screen where need to check if password field exists or not let password is represented by id as pswrd use the below command List<WebElement> pwd = driver.findElements(By.id("pswrd")); if(pwd.size()>0{ // password field exists; }else{  // password field does not exists; }

Moving between frames...

Consider an application where the contents are in  different frames  and need to fetch data from a frame FRAME-1 FRAME-2 FRAME-3 In the above example consider that after selecting an item in frame 2 the contents are displayed in frame 3 now need to read the data in frame 3 public void default_frame(){ try{ driver.switchTo().defaultContent(); frames(Frame1,fmset); }catch(Exception e){ e.printStackTrace(); } } In the above code first we are moving to default frame by switching to default content public void frames(final String x, final String y){ try{ driver.switchTo().frame(x); System.out.println(":::::::::: Frame 1 is switched " + x); driver.findElement(By.tagName(y)); System.out.println(":::::::::: Frameset value " + driver.findElement(By.tagName(y)) + "::::"); }catch(Exception e){ e.printStackTrace(); } } In the above code after moving to frames, now need to go t...

Changing driver (i.e. from Chrome driver to firefox driver) from properties file

Create a property file (property) In the property file enter Webdrivername = org.openqa.selenium.chrome.ChromeDriver Next time if need to run script in say IE, Firefox, Opera just change the content in property file. Below is the Java code which can be used... public WebDriver driver  = browserDriver(); public static WebDriver drv  = null; public static WebDriver browserDriver() { File file1 = new File(/*The path for property the driver of that browser */); if(drv==null){ try{ switch (sdriver) { case "chrome": File FileChrome  = new File(file1+"/chromedriver.exe"); System.setProperty("webdriver.chrome.driver", FileChrome.getAbsolutePath()); browserName("org.openqa.selenium.chrome.ChromeDriver"); break; case "ie": File fileIE = new File(file1+"/IEDriverServer.exe"); System.setProperty("webdriver.ie.driver", fileIE.ge...

Clicking a web element on mouse-over

Below is the sample code which helps for mouseover in selenium webdriver public void mouseover(String x){      WebElement wel = driver.findElement(By.id(x)); System.out.println(":::::::::: String the id is :"+x); Actions builder = new Actions(driver); builder.moveToElement(wel).click(wel); builder.perform(); }

Moving between Windows in selenium

Upon clicking let us assume that a new window page is displayed public void stringhandles(){ Set<String>handles = driver.getWindowHandles(); System.out.println("popopopo"+handles.size()); System.out.println("popopopo"+handles); for(String handle:handles){ for(i=0;i<handles.size();i++){ System.out.println( i); if(i==0){ continue; } } driver.switchTo().window(handle); } }

getting future date

Date part ()  public static Calendar cldr; public static String f_date; public static  Calendar d = Calendar.getInstance(); public static SimpleDateFormat sdf1 = new SimpleDateFormat("MM/dd/yyyy"); public void datepart(){ try { cldr = (Calendar) d.clone(); if(cldr.get(Calendar.DAY_OF_WEEK)<=5){ cldr.add(Calendar.DAY_OF_WEEK,+1); f_date = sdf1.format(cldr.getTime()); System.out.println("PPPPPPPPPP "+ f_date); } else if(cldr.get(Calendar.DAY_OF_WEEK)==6){ cldr.add(Calendar.DAY_OF_WEEK,+3); f_date = sdf1.format(cldr.getTime()); System.out.println("QQQQQQQQQQQ "+ f_date); } else if(cldr.get(Calendar.DAY_OF_WEEK)==7){ cldr.add(Calendar.DAY_OF_WEEK,+2); f_date = sdf1.format(cldr.getTime()); System.out.println("RRRRRRRRR "+ f_date); } else if(cldr.get(Calendar.DAY_OF_WEEK)==0){ cldr.add(Calendar.DAY_OF_WEEK,+1); f_date = sdf1.format(cldr.getT...

How to capture screen while running test suite

Create a file path file1 location where  the image needs to be stored SimpleDateFormat sdf = new SimpleDateFormat("dd_MM_yyyy_HH_mm_ssaa") Date date=new Date(); public void screenshot() { try { System.out.println("Capturing screen........."); String filepath = file1+"\\image"+sdf.format(date)+new Date().getTime()+".png"; File srcfile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); System.out.println("capturing.............."); FileUtils.copyFile(srcfile, new File(filepath)); Count++; String link1 = "<a href = "+filepath+">screenshot"+Count+"</a>"; org.testng.Reporter.log(link1); System.out.println("Image Captured"); } catch (IOException e) { e.printStackTrace(); } } In the above code the Just a System.out.println in the console location where the captured file is stored  This is a selenium command, where t...

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

What is WebDriver?

WebDriver is  a clean and fast framework for automated testing of webapps

Why WebDriver is needed when selenium already exists?

As Selenium is written in JavaScript which causes a significant weakness.  Browsers impose a pretty strict security model on any JavaScript that they execute in order to protect a user from malicious scripts. Example is IE prevents JavaScript from changing the value of an INPUT file element Also,  the  API for Selenium RC  has grown over time, so it has become harder to understand how best to use it. Because of above situation, WebDriver was introduced . WebDriver  takes a different approach to solve the same problem as Selenium.  Instead of using JavaScript application running within the browser,  it uses whichever mechanism is most appropriate to control the browser.  By changing the mechanism used to control the browser, we can circumvent the restrictions placed on  the browser by the JavaScript security model.  In those cases where automation through the browser isn't enough, WebDriver can make use of facil...