Posts

Showing posts with the label WebDriver

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

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

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