How can I integrate selenium tests with sauce labs in c#?

515 Views Asked by At

On saucelabs website they provide a code snippet like this:

WebDriver webDriver = new WebDriver();
webDriver.set(new RemoteWebDriver(
    new URL("https://UrlHEREagwgwgqwg4894+4+91gwgq")
))

When I add this to my tests, under WebDriver it says type or namespace 'WebDriver' could not be found. It is coming up for a namespace. For my Selenium tests I am using IWebDriver. I tried to change the WebDriver to IWebDriver and that didn't work. I also get the same error under the URL saying the namespace could not be found. For that one it does show for a namespace using System.Security.Policy; If I add that then I get an error under

 new URL("https://UrlHEREagwgwgqwg4894+4+91gwgq")

Argument 1: cannot convert from 'System.Security.Policy.Url' to 'OpenQA.Selenium.DriverOptions'

This is how I am trying to use it. I was using ChromeDriver for my selenium tests but commented that part out to test on other browsers with saucelabs. This is my first time working with selenium/saucelabs so what I am doing my be completely off and I appreciate any advice.

 [Fact]
        public static void ClickDownloadButton()
        {
            WebDriver driver = new WebDriver();
            driver.set(new RemoteWebDriver(
            new Url("https://UrlHEREagwgwgqwg4894+4+91gwgq")
            ));
           
            //using IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl(BaseUrl.downloadsUrl);

            var login = new Login(driver);
            login.EnterEmail();
            login.EnterPassword();
            login.HitSubmit();

            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
            
            var downloadButton = wait.Until((d) => d.FindElements(By.LinkText("Download File")));

            foreach (var button in downloadButton)
            {
                IWebElement element = wait.Until((d) => d.FindElement(By.LinkText("Download File")));
                element.Click();
            }
            driver.Quit();
        }

Here are the using statements:

using Xunit;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using System.IO;
using System.Security.Policy;
using OpenQA.Selenium.Remote;
1

There are 1 best solutions below

3
Nikolay Advolodkin On BEST ANSWER

Check out our demo C# repository with tons of examples.

Here's a working example that you can use from the following file:

using System;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;

namespace Selenium3.Nunit.Scripts.SimpleExamples
{
    [TestFixture]
    [Category("SimpleTest")]
    public class SimpleSauceTest
    {
        IWebDriver _driver;
        [Test]
        public void SimpleTest()
        {
            //TODO please supply your Sauce Labs user name in an environment variable
            var sauceUserName = Environment.GetEnvironmentVariable(
                "SAUCE_USERNAME", EnvironmentVariableTarget.User);
            //TODO please supply your own Sauce Labs access Key in an environment variable
            var sauceAccessKey = Environment.GetEnvironmentVariable(
                "SAUCE_ACCESS_KEY", EnvironmentVariableTarget.User);

            ChromeOptions options = new ChromeOptions();
            options.AddAdditionalCapability(CapabilityType.Version, "latest", true);
            options.AddAdditionalCapability(CapabilityType.Platform, "Windows 10", true);
            options.AddAdditionalCapability("username", sauceUserName, true);
            options.AddAdditionalCapability("accessKey", sauceAccessKey, true);
            options.AddAdditionalCapability("name", TestContext.CurrentContext.Test.Name, true);
            options.AddAdditionalCapability("build", "ShwabTeamName:" + DateTime.Now, true);


            _driver = new RemoteWebDriver(new Uri("https://ondemand.saucelabs.com/wd/hub"), options.ToCapabilities(),
                TimeSpan.FromSeconds(600));
            _driver.Navigate().GoToUrl("https://www.google.com");
            Assert.Pass();
        }

        [TearDown]
        public void CleanUpAfterEveryTestMethod()
        {
            var passed = TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed;
            ((IJavaScriptExecutor)_driver).ExecuteScript("sauce:job-result=" + (passed ? "passed" : "failed"));
            _driver?.Quit();
        }
    }
}

Don't forget to install the correct Nuget packages so you have the corresponding resources. This is the minimum that you'll need:

  <package id="Selenium.Support"  />
  <package id="Selenium.WebDriver" /> 

Side Note: Don't use static methods as it'll make it impossible for you to parallelize.