Powered By Blogger

Search Here!

Showing posts with label Selenium webdriver. Show all posts
Showing posts with label Selenium webdriver. Show all posts

Tuesday, April 24, 2012

Selenium Android Testing : Using the remote WebDriver server in C# !

Following are the steps to setup environment : ANDROID TESTING using c#

1. Install SDK and install 4.0.3 Android API's.

2. Install AVD Manager and create AVD using 4.0.3 Android API'.

3. Start AVD by clicking on Start button and launch virtual device.

4. Open command prompt and browse the path of SDK directory under platform-tools folder to run the adb commands for example I had set the path D:\android-sdk-windows\platform-tools

5. Every device or emulator has a serial ID. Run this command to get the serial ID of the device or emulator you want to use and check whether to check the active state:
> adb devices

6. Download the Android server apk file 'android-server-2.0b3-patched2.apk' from http://code.google.com/p/selenium/downloads/list. To install the application do:
> adb -s emulator-5554 -e install -r D:\android-sdk-windows\platform-tools\android-server-2.0b3-patched2.apk

7. Start the Android WebDriver application through the UI of the device, click on WebDriver link button,

or by running this command: 

> adb -s emulator-5554 shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity

 

and confirm Jetty Server started, if this confirmation fails on the emulator UI then run these commands,
> adb kill-server

> adb start-server
then re-start from step 3 again till then you would not get confirmation.

8. Now we need to setup the port forwarding in order to forward traffic from the host machine to the emulator. In a terminal type:
> adb forward tcp:8080 tcp:8080

 9. Start Selenium Server on the machine :
> java -jar selenium-server-standalone-2.0b2.jar -proxyInjectionMode -trustAllSSLCertificates -log selenium.log

10. Now, execute the C# code for this built setup:

[TestClass] 

public class AndroidApp 
{
private IWebDriver WebDriver; 


[TestInitialize] 
public void TestInitializeAttribute() 
{ 
try 
{ 
WebDriver = new AndroidDriver(); 
WebDriver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30)); WebDriver.Manage().Cookies.DeleteAllCookies(); WebDriver.Navigate().GoToUrl("http://google.co.in");
} catch (Exception) { throw; } }


Now, you have done successfully and ready to start testing:)
 

Note: Always use the correct version of Android API with *.apk file.

Selenium WebDriver: Wait Until Element

How to wait for an element till it comes on the page. C# code to wait for an element in a very generic manner using Selenium WebDriver.

public void WaitUntilElement(By by)
{
Boolean elementFound = false;
Stopwatch sw = new Stopwatch();
sw.Start();
int minutesToWait = Int32.Parse(ConfigurationManager.AppSettings["WaitUntillElementMinutes"]);
while (sw.Elapsed.Minutes < minutesToWait)
{
try
{
var elements = WebDriver.FindElements(by);
if (elements != null && elements.Count > 0)
{
elementFound = true;
break;
}
}
catch (Exception)
{ }
}
if (!elementFound)
{
throw new TimeoutException(string.Format("The expected element '{0}' has not found nder specified time interval of '{1}' minute(s).", by, ConfigurationManager.AppSettings["WaitUntillElementMinutes"]));
}
sw.Stop();
sw = null;
}