Showing posts with label Web Automation. Show all posts
Showing posts with label Web Automation. Show all posts

Tuesday, July 7, 2015

EASY TO GET SAFARI WEBDRIVER RUNNING ON MAC OSX


Since this topic has not been well documented on the net and I struggled myself to get Selenium tests running on Safari browser, here are the complete set of steps to get Selenium tests up and running on Safari browsers. :-


Creating “Safari Extension” Developer Certificate

  1. Create an Apple developer account.
  • Go to https://developer.apple.com/
  • Click on “Member Center” on top panel.
  • Select “Create Apple ID”.
  • Go through the sign up forms and provide valid fields for a succesful sign up
2.  Login to created Apple account.
3.  Sign up for “Safari Extensions” developer
  • Click on “Certificates, Identifiers & Profiles”
  • In the “Safari Extensions’ section, click on “Join Now”
  • Go through the following steps and provide valid fields to successfully sign up for the “Safari Extensions” program
4.  Create “Developer Certificate”
  • Now inside “Certificates, Identifiers & Profiles” > “Click on Create/Add Safari Certificate”
  • You will see the following page – “About Creating a Certificate Signing Request (CSR)” 

  • Click on “Continue”


You will see the following page :-


  • Upload the CSR file and Click on ‘Generate’ to generate the certificate
  • Now download the certificate once it is generated.
5. Download the certificate and install in the machine
  • Download the certificate in the download tab.
  • The certificate is downloaded as “safari_extension.cer”.
  • Double-click on the file to install the certificate in the Mac client/OSX.
6.  We have installed the safari extensions certificate for developers. Now we need to install the Safari Webdriver extension for the Safari Browser.

 Installing the Safari Webdriver extension in the Safari Browser

  1. Download latest Selenium Safari extension.
2.  Install the Safari Extension
  • Double-click on the “SafariDriver.safariextz” file.
  • You will get a prompt asking “Are you sure you want to install the extension “WebDriver”?“.
  • Click on “Install”
3.  Provide the default setting for the Selenium Webdriver Extension.
  • Click on “Safari” > “Preferences” > “Extensions” > You will find Selenium extension
  • Select “Enable Webdriver”
Now all the settings are done and now we should be able to launch our Selenium scripts using Safari Webdriver.

Launching Safari Webdriver

Webdriver driver = new SafariDriver();                
driver.get( “http://www.google.co.in” );
builder = new Actions(driver);
This should launch your safari browser with the Safari Webdriver Extension. :)
Hope this article comes of use to you all !!

Happy Coding !!



Credits: itisatechiesworld

Thursday, July 2, 2015

SELENIUM : EASY TO VERIFY PDF FILE TEXT USING JAVA WEBDRIVER

In this post we will explain the procedure to verify PDF file content using java webdriver. As  some time we need to verify content of web application pdf file, opened in browser.
Use below code in your test scripts to get pdf file content.

 //get current urlpdf file url
 URL url = new URL(driver.getCurrentUrl());
                 
 //create buffer reader object
 BufferedInputStream fileToParse = new BufferedInputStream(url.openStream());
 PDFParser pdfParser = new PDFParser(fileToParse);
 pdfParser.parse();

 //save pdf text into strong variable
 String pdftxt = new PDFTextStripper().getText(pdfParser.getPDDocument());
                 
 //close PDFParser object
 pdfParser.getPDDocument().close();

After applying above code, you can store all pdf file content into “pdftxt” string variable. Now you can verify string by giving  in put.  As if you want to verify “Selenium Or Webdiver” text. Use below code.
                 
Assert.assertTrue(pdftxt.contains(“Selenium Or Webdiver”))

Hope this post will help  to verify web application PDF content.


Credits: roadtoautomation blog

Wednesday, July 1, 2015

SELENIUM : WAITING FOR CONDITIONS TO BE AVAILABLE


While testing a very slow loading website, the dreaded "NoSuchElementException" exception can occur even though the element does exist on the page. The problem can be that the code is running faster than the site loads. The "ExpectedCondition" class ensures that the program waits for a designated period of time before throwing the "NoSuchElementException" error message.


In the code below, we navigate to craigslist, take 5 seconds to wait for the required element to appear and be ready for clicking. Then click the element.

WebDriver driver = new FirefoxDriver();

// a slow loading website is good for this text. craigslist is not slow though :)

driver.get("http://london.craigslist.co.uk/");  

// define timeout for waiting period

WebDriverWait wait = new WebDriverWait(driver, 5);    

// ensures the element is available for click. Check for the defined 5 seconds before timeout

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='jjj']/h4/a")));   

element.click();

Tuesday, June 30, 2015

SELENIUM : RERUN FAILED TEST SCRIPTS USING TESTNG


There will be situations where we will re run failed automation test scripts and get the positive results upon rerun.


What if we have some utility which will rerun each failed test script..?


Yes.., We have it..we can automate rerunning test scripts scenario using IRetryAnalyzer and IRetryAnalyzer  interfaces of TestNG.


Create a Java class say RetryTest which implements IRetryAnalyzer  interface of TestNG.


Here, retryDemo() is my test method which will reexecuted on failure.
We are overriding retry() method of IRetryAnalyzer interface.
We are overriding transform() method of IAnnotationTransformer interface.


import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.IAnnotationTransformer;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.annotations.ITestAnnotation;
import org.testng.annotations.Test;

 public class RetryTest implements IRetryAnalyzer,IAnnotationTransformer {

     private int retryCount = 0;
    private int maxRetryCount = 1; //Mention how many times we want to rerun the failed test case.
   
    @Override
    public boolean retry(ITestResult result) {
     if (retryCount < maxRetryCount) {
      System.out.println("Retrying test " + result.getName()
        + " with status " + getResultStatusName(result.getStatus())
        + " for the " + (retryCount + 1) + " time(s).");
      retryCount++;
      return true;
     }
     return false;
    }
   

     @Override
    public void transform(ITestAnnotation testannotation, Class testClass,
      Constructor testConstructor, Method testMethod)    {
     IRetryAnalyzer retry = testannotation.getRetryAnalyzer();

      if (retry == null)    {
      testannotation.setRetryAnalyzer(AutomationFrameWork.class);
     }

     }
   
   
    @Test(retryAnalyzer = RetryTest.class)
    public void retryDemo() throws Exception {
     WebDriver driver = new FirefoxDriver();
     driver.get("https://www.google.co.in");
     WebElement searchBtn = driver.findElement(By.xpath("//input[@name='btnK']"));//get webelement of search button on Google Home Page.
     String actual = searchBtn.getAttribute("value");//Value: Google Search
     String expected = "Yahoo";
     Assert.assertEquals(actual, expected);
   
    }
   
    public String getResultStatusName(int status) {
     String resultName = null;
     if (status == 1)
      resultName = "SUCCESS";
     if (status == 2)
      resultName = "FAILURE";
     if (status == 3)
      resultName = "SKIP";
     return resultName;
    }

   

 }


Thats all folks…!


You have to just modify the test method: retryDemo() based upon need and enjoy.


Happy Coding !