Part I: about seting up the environment
1. I use C# language for writing test case.
2. Install Visual Studio 2015 like picture below:
3. Using NuGet on Visual Studio to install Nunit, selenium support and selenium webdriver three packages like picture below:
4. Until this step the environment is ok, you can start to write test cases.
5. If you do not know how to use selenium api and nunit . please buy related book or e-book to learn. no worry, it is easy to learn.
Part II : about using SauceLabs
1. SauceLabs instruction:
Saucelabs is a test platform it can support test moblie and Web UI and so on . it can support all kinds all language such as C# ,Java, Node.js, PHP, Python,Ruby.
2. Why we choose saucelabs to test ,the advantagesas below:
- Saucelabs has all kinds of browsers it support run against multiple
browers parallelly. - Saucelabs can run test cases fast only waste less time.
- Integrating a CI platform with Sauce Labs through sauce plugins and you can use jenkins or Bamboo CI platform and so on.
3.What are kinds of test can we use saucelabs
In all , saucelabs can support two kinds of test. one is manual test,the others is Auto test.
3.1 Maual test picture:
3.2 Auto test picture:
4. Using saucelabs to start auto test what things are you need to know?
- you need sig up a saucelab account. of course the free account what you sig up only have 90 minute to use.
- you should choose one of language for write test case. such as C# ,Java, Node.js, PHP, Python,Ruby. you can choose one of them what you like .
- write the username and accessKey of saucelabs account into your test code . I use C# language like below:
desiredCapabilites.SetCapability("accessKey", "2f11bc66-e756-4785-896b-4c6aba92e75c");
desiredCapabilites.SetCapability("username", "testlin_2016");
Part III : about run test case
1. Run test case throught jenkins
- Firstly, install jenkins and new jenkins job for test cases .
- Secondly install Sauce OnDemand Plugin on jenkins. about this you can follow the url below to install and configure on jenkins.
(https://wiki.saucelabs.com/display/DOCS/Installing+and+Configuring+the+Sauce+OnDemand+Plugin+for+Jenkins,
(https://wiki.saucelabs.com/display/DOCS/Configuring+Sauce+Connect+with+the+Jenkins+Plugin)
please feel free , it is easy to do . - If you install and configure succeed.you can see the picture on jenkins job Configure like picture below:
-
Util now you can click the button to run test case like picture below:
-
Of course, there is a good way that is set run time and use ant script to run your testcase on jenkins automatically.
set time like picture below:
use ant script like picture below:
-
At last you can write the username and accessKey of saucelabs account into your test code like below instead of above.
desiredCapabilites.SetCapability("username",Environment.GetEnvironmentVariable("SAUCE_USER_NAME"));
desiredCapabilites.SetCapability("accessKey", Environment.GetEnvironmentVariable("SAUCE_API_KEY"));
By the way if you do not know what are the SAUCE_USER_NAME and SAUCE_API_KEY .please go for this url below:
https://wiki.saucelabs.com/display/DOCS/Environment+Variables+Used+by+the+Jenkins+Plugin.
Part IV: Problems I meet
1. I can not access the svg element on web page.
solution: I use javascript in C#
public String GetEachAttributeName(IWebDriver driver, String objectString)
{
IWebElement svgObject = driver.FindElement(By.Id("svgobj"));
IJavaScriptExecutor jsdriver = (IJavaScriptExecutor)driver;
String result = jsdriver.ExecuteScript(objectString, svgObject).ToString();
return result;
}
public String GetNameJsString(String firstElement, String secondelement)
{
var getNameJsString = "var svg = arguments[0].contentDocument.getElementById('" + firstElement + "'),texts=svg.getElementsByTagName('" + secondelement + "');return texts[0].textContent;";
return getNameJsString;
}
###2. Sometimes selenium can not find the element.
solution: use try … catch to deal with exception.
public bool IsWebElementDisplayed(IWebElement driver, By selector)
{
bool isDisplayedFlag = false;
int loopCount = 0;
int maxLoopCount = 8;
while (!isDisplayedFlag && loopCount< maxLoopCount)
{
loopCount++;
try
{
IWebElement element = driver.FindElement(selector);
isDisplayedFlag = element.Displayed;
Console.WriteLine("IsWebElementDisplayed loopCount:{0},isDisplayedFlag: {1}", loopCount, isDisplayedFlag);
return isDisplayedFlag;
}
catch (StaleElementReferenceException)
{
Thread.Sleep(1000);
return false;
}
}
Console.WriteLine("IsWebElementDisplayed loopCount:{0},isDisplayedFlag: {1}", loopCount, isDisplayedFlag);
return isDisplayedFlag;
}
In all , Web Ui test is easy to start. please no worry and you can follow this instruction step by step. good luck!