Friday 6 January 2017

How to Open New Tab with Selenium WebDriver

Selenium WebDriver  tool do not have any built In API using which we can directly use to open new tab. Normally we are using CTRL + t Keys to open new tab In Browser and cltr + tab to switch between the tabs. We will use this in our selenium test case to open another tab inside a browser and switch to tab. 

Check out the Example below in details to understand how to open a new tab, how to open particular url in the new tab, how to switch between two tabs  and how to close the tabs.




import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class BromiumTests {

 WebDriver driver;
 Utils util;

 @BeforeTest
 public void setUp() {
  System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  
 }

 
 
 @Test
 public void testOpenYoutube() {
  driver.get("https://www.youtube.com");
  
  driver.get("https://www.youtube.com/watch?v=9NXEnGiOeUU");
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 }
        @Test
 public void testOpenGmail() {
  switchToNewTab();
  driver.get("http://www.gmail.com");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }
       public void openNewTab() {
  ((JavascriptExecutor) driver).executeScript("window.open('about:blank','_blank');");
 }

 public void switchToNewTab() {
  openNewTab();
  String subWindowHandler = null;

  Set<String> handles = driver.getWindowHandles();
  Iterator<String> iterator = handles.iterator();
  while (iterator.hasNext()) {
   subWindowHandler = iterator.next();
  }
  driver.switchTo().window(subWindowHandler);
 }

 @AfterClass
 public void tearDown() {
  driver.quit();
 }
 

}

How to Capture ScreenShot with Selenium WebDriver

Taking screenshot while testing is very important for automation so that you can point out and catch the exact bug. In this post we will learn about how to take screen shot when selenium test case fails. It is more important to have a screenshot when the selenium test fail but you can add the code if you want to capture screen shot when selenium test passes.
Once you know how to use it then you can use it anywhere you want. So lets get started . . .


Take ScreenShot with Selenium WebDriver

Video Tutorial Is Also Available -


import java.io.File;

import java.io.IOException;

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

public class ScreenshootGoogle {

 @Test
 public void TestJavaS1()
{
// Open chrome browser
 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();

// Maximize the window
driver.manage().window().maximize();

// Pass the url
driver.get("http://www.qaautomated.com");

// Take screenshot and store as a file format
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
 // now copy the  screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("C:/selenium/error.png"));
}

catch (IOException e)
 {
  System.out.println(e.getMessage());

 }
 }

Thursday 5 January 2017

How to Handle App Permissions popup in Appium Test

We have faced this issue of Marshmallow Android Devices that apps shows permission popups asking for user permission to allow or deny. To handle this inside our automated test case is required. So this post will explain how we can handle this with simple code.
Consider we have an mobile app which asks for all required permission at the time of launch and we need to click on allow button each time it displays the App permission popup.


Check the below Test Case Example where we are calling allowAppPermission() method to click on each app permission popup. Let us see how the method internally works. In this method we have added a while loop until we get the allow app permission button . To locate the allow button we have used MobileBy api which is provided by lastest Appium Java Client.

Video Tutorial -

 




import java.net.MalformedURLException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;



public class WaitTests {

 WebDriver driver;

 @Before
 public void setUp() throws MalformedURLException {
  
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "XT1562");
  capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
  capabilities.setCapability(CapabilityType.VERSION, "6.0.1");
  capabilities.setCapability("platformName", "Android");
  driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  
  
 }

 @Test
 public void testFirst() {
  
   alllowAppPermission();
   driver.findElement(By.name("Login")).click();
   driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
}

@After
 public void End() {
  driver.quit();
 }
public void allowAppPermission(){
 while (driver.findElements(MobileBy.xpath("//*[@class='android.widget.Button'][2]")).size()>0) 

 {  driver.findElement(MobileBy.xpath("//*[@class='android.widget.Button'][2]")).click();
 }
}
 

}

I hope you find this useful. Please share your feedback in comments section below and follow me on social media for latest post updates.

Tuesday 3 January 2017

Jenkins For Appium and Maven Project

Video Tutorial -

Introduction of Jenkins

Jenkins is the leading open-source continuous integration tool developed by Hudson lab. It is cross-platform and can be used on Windows, Linux, Mac OS and Solaris environments. Jenkins is written in Java Programming language.Jenkins is a powerful application that allows continuous integration and continuous delivery of projects, regardless of the platform you are working on. You can integrate Jenkins with a number of testing and deployment technologies. In this tutorial, we will learn Jenkins with Maven project

Important Features of Jenkins

1. Jenkins generates the list of all changes done in repositories like SVN,Github.
2.Jenkins provides direct links to the latest build or failed build that can be used for easy communication.
3. Jenkins is easy to install either using direct installation file (exe).
4. Jenkins can be configured to email the content of the status of the build.
5. To configure various tasks on Jenkins is easy.
6. Jenkins can be configured to run the automation test build on TestNG after each build.
7. Jenkins can be configured to distribute the build on multiple machines.
8. 3rd party plugin can be configured in Jenkins to use features and additional functionality.

Why Jenkins and Appium?

1. Running Appium tests in Jenkins allows you to run your tests every time your software changes.
2. Jenkins can schedule your tests to run at specific time which is good for regression test execution for each build.
3. You can save the execution history reports and Test result reports.
4. Jenkins supports Maven for building and testing a project in continuous integration.

Jenkins Installation


1. First of all download Jenkins from HERE.
2. Install Jenkins.

Install Jenkins

3. Once it is installed open  http://localhost:8080/ url in browser.

Install Jenkins

4. Then navigate to the path and enter the password and click on continue

Install Jenkins

5. Click on Manage Jenkins



6. Click on Manage Plugins

7. Search for "Maven Integration Plugin" and select install without restart option.

8. After Maven Installation is successful we can get the Maven project while adding New Item

9. Go to New Item -> Enter The Name -> Select Maven Project -> Click OK

Jenikins Appium Maven


10. Scroll down to build and you will find option that Jenkins want to know where your maven is installed. then click on the tool configuration.
Jenikins Appium Maven

11. Then add your jdk path and maven path.

Jenikins Appium Maven
12. In the build Root POM give your project pom.xml path.
Jenikins Appium Maven

13. Save the changes and click on Build Now.

Jenikins Appium Maven 
14. Once the Build is executed you can check the build history.