Saturday 20 October 2018

Selennium With BrowserStack

Compatibility Testing Using BrowserStack & Selenium – TestNG


Image result for multiple platform testing

Compatibility Testing is a type of Software testing to check whether your software is capable of running on different hardware, operating systems, applications , network environments or Mobile devices.
Compatibility Testing is a type of the Non-functional testing
BrowserStack allow us to make automation testing on different platforms at once place.
You need to set few parameters browsers and platforms. There are few steps to be followed to integrate Selenium with BrowserStack.
Step1: Sign Up for BrowserStack account
Step2: Get your username and access key.
Step3: Create your test script using TestNG
Step4: Create your testng.xml file to run your test in parallel
Step5: Execute testng.xml
Step6: To view your result, login and click on automate link, so that you can view your project result.
Here is sample code that I am providing written using TestNG.

public class BSTC {
private WebDriver driver;
public static final String USERNAME = “<usename>”;
public static final String AUTOMATE_KEY = “<accesskey>”;
public static final String URL = “http://browserstack.com; + USERNAME + “:” + AUTOMATE_KEY
+ “@hub.browserstack.com/wd/hub”;
@BeforeTest
@Parameters(value = { “browser”, “version”, “platform” })
public void setUp(String browser, String version, String platform) throws exception{
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability(“platform”, platform);
capability.setCapability(“browserName”, browser);
capability.setCapability(“browserVersion”, version)
capability.setCapability(“project”, “TestProject”);
capability.setCapability(“build”, “3.03”);
driver = new RemoteWebDriver(new URL(URL), capability);
}
@Test(priority = 1)
public void TC01() throws Exception {
driver.get(“http://www.google.com;);
Assert.assertEquals(“Google”, driver.getTitle());
WebElement element = driver.findElement(By.name(“q”));
element.sendKeys(“BrowserStack”);
element.sendKeys(Keys.ENTER);
}
@Test(priority = 2)
public void TC02() {
driver.get(“http://seleniumhq.org;);
Assert.assertEquals(“Selenium – Web Browser Automation”,
driver.getTitle());
}
@AfterMethod
public void takeScreenShot(ITestResult result)
{
if (result.getStatus() == ITestResult.FAILURE)
      {
           driver = new Augmenter().augment(driver);
           File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
           FileUtils.copyFile(srcFile, new File(“D:\\Screenshot”+ result.getParameters().toString() + “.png”));
} catch (IOException e)
{
          e.printStackTrace();
}
}
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
}
Now setup your configuration in xml file. Copy the below code.
<suite thread-count=”3″ name=”Suite” parallel=”tests”>
<test name=”FirstTest”>
          <parameter name=”browser” value=”firefox” />
          <parameter name=”version” value=”42″ />
          <parameter name=”platform” value=”Windows” />
          <classes>
                    <class name=”package1.BSTC” />
          </classes>
</test>
<test name=”SecondTest”>
          <parameter name=”browser” value=”safari” />
          <parameter name=”version” value=”7.0″ />
          <parameter name=”platform” value=”MAC” />
          <classes>
                     <class name=”package1.BSTC” />
          </classes>
</test>
<test name=”ThirdTest”>
          <parameter name=”browser” value=”Internet Explorer” />
          <parameter name=”version” value=”10″ />
          <parameter name=”platform” value=”Windows” />
          <classes>
                     <class name=”package1.BSTC” />
          </classes>
</test>
</suite>

This is how, we can execute same test case in multiple environment. Just login and click on 'Automate' link and you will see your result.
All The Best... 

Friday 19 October 2018

How to handle SSL Certificate Error using Selenium Webdriver\

Image result for ssl certificate



\

Suppose we have written test scripts and while running those script, if you encounter situation like "Untrusted Connection" then how do we handle such exception in Selenium automation.

In such cases we have to adjust our script in such way that, it will take care of SSL exception by itself.

Some desired capabilities being used to configure webdriver instance. Through this capabililties one can configure all driver instances like ChromeDriver, FireFoxDriver and Internet Explorer.

SSL Certificate Error Handling in Chrome
To handle such case in Chrome, one needs to use desired capabilities of Selenium webdriver. Below snippet will help to accept all SSL certificate in Chrome. 
DesiredCapabilities handlSSLErr = DesiredCapabilities.chrome ()       
handlSSLErr.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true) 
WebDriver driver = new ChromeDriver (handlSSLErr); 

SSL Certificate Error Handling in Firefox

To handle such case in Chrome, one needs to use desired capabilities of Selenium webdriver. Below snippet will help to accept all SSL certificate in Chrome. 

1) Pre-requisite is, one needs to create new firefox profile say "myProfile" 
2) Now access myProfile in the script as below and create it's object.

ProfilesIni prof = new ProfilesIni()    
FirefoxProfile ffProfile= prof.getProfile ("myProfile")
3) Now we need to configure "SetAcceptUntrustedCertificates" and "setAssumeUntrustedCertificateIssuer" properties in the FireFox  profile

ffProfile.setAcceptUntrustedCertificates(true) 
ffProfile.setAssumeUntrustedCertificateIssuer(false)
4) Now use FireFox profile in FireFox driver instance.

WebDriver driver = new FirefoxDriver (ffProfile)   
Note: "setAcceptUntrustedCertificates" and "setAssumeUntrustedCertificateIssuer" are capabilities to handle the certificate errors in web browsers.


---------------------------

Select Language