1

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;
dev_in_training
  • 333
  • 5
  • 16

1 Answers1

1

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.

Nikolay Advolodkin
  • 1,820
  • 2
  • 24
  • 28
  • Thank you for the example. If I want to test multiple browsers how would I do that? It looks like that example would only be for chrome. Also I have my selenium tests using x-unit. Can the sauce labs be used with x-unit or does it have to be nunit? – dev_in_training Mar 11 '21 at 13:56
  • 1
    Cross browser like this https://github.com/saucelabs-training/demo-csharp/blob/063ddaab3607472d1238b72c6dd800772f24a489/SauceExamples/Web.Tests/BestPractices/test/LoginFeature.cs#L8 . Yes, Sauce supports all testing libraries. However, you'll need to figure out how to replicate the code I shared with Xunit. Xunit isn't a very popular library so we don't write examples for it. If you decide to use more popular libraries like Nunit or MsTest, all the examples you could ever want exist here https://github.com/saucelabs-training/demo-csharp – Nikolay Advolodkin Mar 12 '21 at 16:01
  • @dev_in_training if my answer solved your problem, please don't forget to mark it as the answer and upvote to help others – Nikolay Advolodkin Mar 12 '21 at 16:03