Sunday 9 December 2018

Selenium with C# mobile

Your guide to running Selenium Webdriver tests with C# on BrowserStack.


Introduction

BrowserStack gives you instant access to our Selenium Grid of 2000+ real devices and desktop browsers. Running your Selenium tests with C# on BrowserStack is simple. This guide will help you:
  1. Run a sample Selenium Webdriver test on BrowserStack
  2. Setup your environment to be able to test URLs in your internal network
  3. Understand and configure the core capabilities in your Selenium test suite
  4. Explore advanced features

Prerequisites

Before you can start running your Selenium tests with C#::
  1. Download the Selenium C# bindings from the Selenium website
  2. Extract the bindings and add them to relevant folder

Getting Started

Note: Running your Selenium tests on BrowserStack requires a username and an access key.
To get started, let’s run a simple Selenium Webdriver test. The C# script below will open a URL, input a string, submit the form, and return the page title.
First, select the OS and Device/Browser combination you'd like to test on the using drop-down menus below. This will automatically update the C# code sample below:
1. Select an OS
2. Select a device
Look for the  icon to select a real device.
Note: Testing on real devices requires the Automate Mobile plan
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace SeleniumTest {
  class Program {
    static void Main(string[] args) {
      IWebDriver driver;
      DesiredCapabilities capability = new DesiredCapabilities();
      capability.SetCapability("browserName", "iPhone");
      capability.SetCapability("device", "iPhone 8 Plus");
      capability.SetCapability("realMobile", "true");
      capability.SetCapability("os_version", "11.0");
      capability.SetCapability("browserstack.user", "USERNAME");
      capability.SetCapability("browserstack.key", "ACCESS_KEY");

      driver = new RemoteWebDriver(
        new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability
      );
      driver.Navigate().GoToUrl("http://www.google.com");
      Console.WriteLine(driver.Title);

      IWebElement query = driver.FindElement(By.Name("q"));
      query.SendKeys("Browserstack");
      query.Submit();
      Console.WriteLine(driver.Title);

      driver.Quit();
    }
  }
}