SauceLabs + Selenium+ Nunit For Web Ui Test

来源:互联网 发布:java接口开发demo 编辑:程序博客网 时间:2024/05/22 09:42

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.

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!

2 0
原创粉丝点击