Currently Browsing

Posts Tagged ‘ TestNG ’

Change iOS Settings on virtual device with Appium

In our previous post we introduced the steps to set up your Appium environment for iOS 9 devices. In this post we will write about how can you open the Settings app and do some changes in it. We will use Java as programming language, Eclipse for IDE and JUnit as runner.

Ok, so first step is to get the Java client of Selenium from www.seleniumhq.org/download/. Extract the downloaded *.zip file, create a new Java Project in Eclipse and add all extracted *.jar files to Build Path. Now, we can start to implement our first test case for iOS device where we will open the Settings app and disable the Location Service on iPhone. As we mentioned above, we will use JUnit as runner, but you can also choose TestNG or just create a standard Java program with the main() function.

Let’s create a before() method with annotation @Before where we will set up all of the necessary things for the desired test case.

Create an Ant build for TestNG tests

The goal of this exercise is to run TestNG tests through Apache Ant.

First you have to have a sample TestNg project at your hand.
If you don’t have any, you can create the sample using the instructions below, or you can download it from using the link from the bottom of the post.
Create a simple class Exampletest.java

package main;

import org.testng.annotations.Test;

public class ExampleTest {

	@Test
	public void testA() throws Exception {

		System.out.println("Test finished.");

	}
}

Emulate mobile and tablet devices with ChromeDriver

Desktop version of Google Chrome allows to users to open webpages in mobile view. This means that you can emulate browsing on devices like Google Nexus 5, Apple iPhone 6 etc. You can find this feature in developer tools by clicking on the mobile icon.

Mobile emulation button

Mobile emulation button

Now ChromeDriver allows to use device emulation in automated tests also. This feature speeds up web development, allows developers to quickly test how a website will render on a mobile device, without requiring a real device or a virtual machine. You can find more information about it on the following link https://developer.chrome.com/devtools/docs/device-mode. The setup is very easy and it’s working really well. Below you can find a little test case which will introduce to you how to use ChromeDriver for emulate a mobile device and tablet. The test case simply enters “what is my browser” into the google search page, clicks on the first result and checks the device type what will be present on the website.

@DataProvider(name = "deviceName")
	public Object[][] deviceNames(){
		return new Object[][] {
				{"Google Nexus 5", "LG Nexus 5"},
				{"Apple iPhone 6", "Apple iPhone"},
				{"Google Nexus 10", "Samsung Nexus 10"}
		};
	}

	@Test(dataProvider = "deviceName")
	public void mobileEmulation(String deviceName, String deviceNameWhatIsMyBrowser){
		Map<String, String> mobileEmulation = new HashMap<String, String>();
		mobileEmulation.put("deviceName", deviceName);

		Map<String, Object> chromeOptions = new HashMap<String, Object>();
		chromeOptions.put("mobileEmulation", mobileEmulation);
		DesiredCapabilities capabilities = DesiredCapabilities.chrome();
		capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
		WebDriver driver = new ChromeDriver(capabilities);

		driver.get("http://www.google.com/ncr");
		//Enter my search
		driver.findElement(By.cssSelector("#lst-ib")).sendKeys("what is my browser");
		//Click on search button
		driver.findElement(By.cssSelector("#tsbb")).click();
		//Click on the first found result
		driver.findElement(By.xpath("//h3[@class='r']/a[text()='What browser am I using? Is my browser up to date? Web browser ...']")).click();
		//Get the device name from the website
		String deviceNameFromWebsite = driver.findElement(By.cssSelector(".string-extra-info")).getText();
		//Check the device name from the website
		Assert.assertEquals(deviceNameFromWebsite, deviceNameWhatIsMyBrowser, "The device name is not the expected one.");
		
		driver.quit();
	}	

How to run parallel tests with Selenium WebDriver and TestNG in Chrome browser and in Internet Explorer browser

In the previous lesson(How to run parallel tests with Selenium WebDriver and TestNG) we used Firefox browser to run the tests.
Now we introduce how to run the tests parallel in Chrome and in Internet Explorer browsers.

Firstly download the latest chromedriver and IEDriverServer files.
You can download the IEDriverServer file from the http://code.google.com/p/selenium/downloads/list site. My opinion is that use the 32-bit version, because the 64-bit version is in some cases are too slow.
The chromedriver download site is http://code.google.com/p/chromedriver/downloads/list .
In this case my opinion is that use the chromedriver_win_26.0.xxxx version (this version is released in Jan 2013). There are newer versions, like chromedriver_win32_2.1 (Jul 9 2013) and maybe this version is good, but the previous versions, like 2.0, are not worked perfectly.

Put the downloaded and unzipped files to a folder.
In Advanced system settings/ Environment Variables update the Path:
In to the Path variable add:
–    The location of the two file (in my case):      C:\SeleniumDrivers;

If you want to use Chrome browser, follow the steps:
Now (as in the previous lesson) open command prompt (with administrator privileges).
Go to c:\ and enter the following line to start Selenium Grid:
java -jar selenium-server-standalone-2.33.0.jar -role hub -port 4444
Open a new command prompt window, go to c:\ and enter the following line to register the browser to the Selenium Grid:
java -jar selenium-server-standalone-2.33.0.jar -role node -hub http://localhost:4444/grid/register -browser browserName=chrome,maxInstances=3
Here is the code for it:

How to run parallel tests with Selenium WebDriver and TestNG

First install Java JRE and JDK.
In Advanced system settings/ Environment Variables setup java path:
Create a new system variable (if it is not created):

Name: JAVA_HOME
Value(the location of the jdk):   C:\Program Files\Java\jdk1.7.0_25

In to the Path variable add:
The location of jre:  c:\Program Files (x86)\Java\jre7\bin\;

Install selenium and testing to Eclipse.
Download (from  code.google.com) the selenium-server-standalone-2.33.0.jar file (or the newest).
Copy this file to a simple place, for example C:\

Now open command prompt (with administrator privileges).
Go to c:\ and enter the following line to start Selenium Grid:
java -jar selenium-server-standalone-2.33.0.jar -role hub -port 4444

Open a new command prompt window, go to c:\ and enter the following line to register the browser to the Selenium Grid:
java -jar selenium-server-standalone-2.33.0.jar -role node -hub http://localhost:4444/grid/register -browser browserName=firefox,maxInstances=3

The maxInstance will determine the max number of the browsers. If it is set to 3 than maximum 3 browsers(tests) are able to run parallel.
(If you want to know more about Selenium Grid visit the Selenium site: http://docs.seleniumhq.org/docs/07_selenium_grid.jsp )

Now in Eclipse create a new project, a new package and a new class. Name it TestBase.
To this file create:
•    a new RemoteWebDriver,
•    to the @BeforeMethod the setUp() function
•    to the @AfterMethod the tearDown() function
•    getDriver() function

How to use TestNG with Selenium WebDriver

(The precondition for this tutorial that the Selenium WebDriver is already installed.)
Firstly go to TestNG website’s download page, and follow the instructions to install the TestNG plugin to Eclipse. (http://testng.org/doc/download.html)

After the TestNG was installed, create a new java class file (without main method).

Create the setUp() function. To this function you can put that the WebDriver use for example the Firefox browser, and for example that the driver get the desired webpage.

This function will start before the test. To ensure that, before the setUp() function need to put @BeforeClass annotation.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

If you use only one test in the java file then you can use the @BeforeTest annotation too.

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.