Sunday, 26 March 2017

Tuesday, 14 March 2017

How to Setup a Hub,Node and parallel execution for Selenium Grid

How to Setup a Hub and Node for Selenium Grid

Selenium Grid allows us to execute our tests in multiple machines (physical / virtual) and multiple browsers with different versions, which dramatically speeds up test execution and helps in reducing total amount of time required for test execution.
For example, if we have a script that takes 100 minutes to execute sequentially , we could break that down to 10 short tests script run across 10 machines, and can complete them in 10 minutes without copying your test code to the other machine.
A grid consists of a single hub, and one or more nodes, Hub and Node are the two main elements that you come across when using grid. Download selenium grid and selenium server.

Hub the Hub is the central point which will receive all the test requests along with information on which browser, platform (i.e. WINDOWS, LINUX, etc) and where the test should be run. Based on the request received, it will distribute them to the registered nodes.

Command to start a hub with default parameters -java -jar selenium-server-standalone-2.48.2.jar -role hub

After starting the hub, we can view the status of the hub by opening any browser window and navigating to: http://localhost:4444/grid/console . If you have used any other port, you need to mention that port value instead of default port 4444

Below is the command to register node with a Hub. If we are not specifying any parameters when starting node, it defaults to 5555 whenever "-role" option is provided and is not hub.

java -jar selenium-server-standalone-2.48.2.jar -role node -hub http://localhost:4444/grid/register

When the hub is running in the same machine, we use ‘localhost’ for node. If Hub and Node are running on separate machines, we have to register Node using the hostname of the remote machine running the hub.
We have done with starting the Hub and Registering Nodes with the Hub. Now we need to run our tests with Selenium Grid, For webdriver nodes, we need to use the RemoteWebDriver and DesiredCapabilities object to define which browser, version of the browser and platform (OS - Windows / LINUX etc) that we want to run our tests.
Based on preferences that we set in the DesiredCapabilities instance, the Hub will point our tests to a node that matches with these preferences. If we specify capabilities that do not exist on our grid then there will be no match and the test will fail to run.
Why we need to use RemoteWebdriver Not the webdriver ?
If we use driver (FirefoxDriver / ChromeDriver / or other) not RemoteWebDriver, it will just assume that the communication to the browser is local. Example: - Webdriver driver = new FirefoxDriver(); Using this, driver will access Firefox browser which is available on the local machine.
If we use RemoteWebDriver, it requires us to specify where the Selenium Server is located and on which web browser we want to execute our tests. For example,
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox());

Execute webdriver tests in Parallel using selenium Grid

We will register multiple nodes to the Hub and execute tests in parallel. In the below example we will register a node in the same local machine where hub is running and other node in remote machine.
We will register node 1 in local machine with the below command:

java -jar selenium-server-standalone-2.48.2.jar -role node -hub 
http://localhost:4444/grid/register
We will register node 2 with the below command in remote machine. In the below command, need to pass a parameter by passing JVM properties using the -D flag along with chrome driver path , so that when ever there is a request to execute in chrome driver, Hub will send the request to this node. And here we have to mention the IP address of the machine where the hub is running as we are starting this node in remote machine.

java -jar selenium-server-standalone-2.48.2.jar -role node -hub 
http://10.0.0.6:4444/grid/register -Dwebdriver.chrome.driver=.\chromedriver.exe


Now to execute these tests, we need to create testng.xml file as below and set parallel="tests" with parameter browser for each test
Create two test classes ParallelTestA and ParallelTestB with different browser @Parameter
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Main Test Suite" parallel="tests" verbose="1">
<test name="Grid firefox Test">
<parameter name="browser" value="firefox" />
<classes>
<class name="com.test.ParallelTestA" />
</classes>
</test>
<test name="Grid chrome Test">
<parameter name="browser" value="chrome" />
<classes>
<class name="com.test.ParallelTestB" />
</classes>
</test>
</suite>


There are many other parameters for browser settings that we can pass when registering node to hub. When ever we use -browser parameter, the default browsers will be ignored and only what we specify in command line will be used.
Example :
-browser browserName=firefox,version=30,maxInstances=5,platform=WIN8_1




Monday, 13 March 2017

Appium execution on real devices USB /WiFi


1     Execution on real devices using USB -

Assume all the below Prerequisites are installed and configured :-
1. JDK should be installed
2. Android should be installed and path should be setup in your machine
3. Appium should be installed.
4. Configure Device with Developer Mode option enabled

In order to run Appium test cases on real devices, we need to connect real android device to PC by enabling USB debugging mode.To test on a real device or on on a simulator, we need SDK to be installed on your machine. Appium will take the advantage of the SDK to connect to the real device connected via USB or simulator.
First Enable Developer Option, To enable Developer Option in android device, Click on Settings and find out ‘About Phone’ option.
Follow below steps –
Step 1 : Click on About Phone option
Step 2 : Click on Version Information
Step 3 : Now you will find an option called ‘Build Number’.
Step 4 : Keep clicking Build number option for seven times. It starts showing you information message as ‘You are now 2 steps away from being a developer’
Step 5 : and again ‘You are now 1 step away from being a developer’
Step 6 : And After clicking for 7 times, finally it will show a message as ‘You are now a developer!’
Step 7 : Now you can see ‘Developer Options’ from where you can enable ‘USB debugging’.
Step 8 : Click on ‘Developer Options’, which will show you multiple options available
Step 9 : Check ‘USB debugging’ option, Once we check that option, it will display warning
Step 10 : Click on OK to continue. Done!!!!! Now we have successfully enabled USB debugging mode for android device.
Finally, To make sure if the device is connected properly with USB debugging mode, we can check using command prompt, Open command prompt and enter ‘adb devices’, which will show you the device connected.
To check this open command prompt and type
abd devices
this will give list of connected devices and there name.
2.       Execution on real devices using WiFi –

Before proceeding further, you should make sure Android SDK is configured in your machine AND should have enabled ‘USB debugging’ from 'Developer Options' in your android device.
Step 1 :- Connect your device to computer via USB and check if it is connected using 'adb devices' which will list you the devices connected to your machine.
Step 2 :- We should make sure that android device and the PC are connected to the same Wifi network.
Step 3 :- Now we need to restart adb and make it work over tcpip by specifying the port value. If no port number is specified, Port 5555 is used by default.

adb tcpip <port>

In the above command, 'adb tcpip' command actually tries to reconfigure and restart the adb daemon on the device. By default in most of the cases it will be using USB. After executing the above command adb daemon will now start listening to tcpip.

Step 3 :- Now disconnect the device which is connected to PC
Step 4 :- We need to get the IP address of the the device. There are two ways to get IP address of your device
Firstly, you can get that from your device -> Settings -> Wifi -> Wifi Settings / You can click on your wifi network which will popup details
Alternatively, we can run the command 'adb shell ip -f inet addr show wlan0' which will show you the ip address of the connected device.

Step 5 :- Run the below command to connect adb to your device over Wi-Fi using IP address of your device:
adb connect device_IP_Address

Now we can execute our tests on Real device over wifi. Let us now connect multiple Android Devices over Wifi and execute our tests.

How to execute Appium Tests On Real Android Device over Wifi with Multiple Devices?

Step 1 :- Connect your device ONE to PC
Step 2 :- "adb tcpip "
Step 3 :- Disconnect the device from the PC
Step 4 :- Now Connect your device TWO to PC
Step 5 :- "adb tcpip "
Step 6 :- Disconnect the device from the PC
Step 7 :- Get the IP addresses of the two devices [explained ways to get IP address above], and run below command to connect adb to your device over Wi-Fi using IP address of your device:

adb connect deviceONE_IP_Address
adb connect deviceTWO_IP_Address

Example : -
adb connect 10.0.0.2 // IP address of device ONE
adb connect 10.0.0.6 // IP address of device Two
Remember, Now again when we want to switch to USB mode, we have to tell the ADB daemon return to listening over USB. Enter below command:
#To Set back the port to USB, so the next time when ADB is started, it will start on USB again.
adb usb

Try the following steps and it should bring your device back.
adb kill-server
adb start-server
adb devices

adb start-server - Which will ensure that there is a server running
adb kill-server - This command Will kill the server if it is running