had to upgrade to the Selenium 4 alpha to get this to work

This commit is contained in:
Greg Gauthier 2020-10-12 23:42:17 +01:00
parent 63606a3970
commit 2e71327e04
4 changed files with 86 additions and 22 deletions

View File

@ -0,0 +1,66 @@
using System;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Safari;
namespace weehawken
{
public static class BrowserDriver
{
public static RemoteWebDriver GetDriver(string browser, bool headless)
{
switch (browser)
{
case "chrome":
return GetChrome(headless);
case "firefox":
return GetFirefox(headless);
case "safari":
return getSafari(headless);
case "edge":
return getEdge(headless);
default:
throw new ArgumentException("Invalid browser specified");
}
}
private static RemoteWebDriver GetChrome(bool headless)
{
ChromeOptions opts = new ChromeOptions();
if (headless)
{
opts.AddArguments("headless");
}
opts.AcceptInsecureCertificates = true;
RemoteWebDriver driver = new ChromeDriver(opts);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
return driver;
}
private static RemoteWebDriver GetFirefox(bool headless)
{
FirefoxOptions fopts = new FirefoxOptions();
if (headless)
{
fopts.AddArguments("--headless");
}
fopts.AcceptInsecureCertificates = true;
RemoteWebDriver driver = new FirefoxDriver(fopts);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
return driver;
}
private static RemoteWebDriver getSafari(bool headless)
{
return new SafariDriver();
}
private static RemoteWebDriver getEdge(bool headless)
{
return new EdgeDriver();
}
}
}

View File

@ -1,7 +1,6 @@
using System;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
@ -9,56 +8,51 @@ namespace weehawken
{
public class Tests
{
ChromeDriver chromeDriver;
FirefoxDriver firefoxDriver;
String url = "https://duckduckgo.com/";
private String url;
private RemoteWebDriver driver;
[SetUp]
public void Setup()
{
Console.WriteLine("Beginning Test");
url = "https://duckduckgo.com/";
}
[TearDown]
public void TearDown()
{
driver.Quit();
Console.WriteLine("End Test");
}
[Test]
public void ChromeTest()
{
Console.WriteLine("Chrome Test");
ChromeOptions copts = new ChromeOptions();
copts.AddArguments("headless");
chromeDriver = new ChromeDriver(copts);
chromeDriver.Navigate().GoToUrl(url);
driver = BrowserDriver.GetDriver("chrome", true);
driver.Navigate().GoToUrl(url);
RemoteWebElement searchBox =
(RemoteWebElement)chromeDriver.FindElementByXPath("//input[@id='search_form_input_homepage']");
(RemoteWebElement)driver.FindElementByXPath("//input[@id='search_form_input_homepage']");
searchBox.SendKeys("frankenberries");
searchBox.SendKeys(Keys.Enter);
RemoteWebElement results =
(RemoteWebElement)chromeDriver.FindElementByPartialLinkText("Monster cereal");
(RemoteWebElement)driver.FindElementByPartialLinkText("Monster cereal");
Assert.NotNull(results);
chromeDriver.Quit();
}
[Test]
public void FirefoxTest()
{
Console.WriteLine("Firefox Test");
FirefoxOptions fopts = new FirefoxOptions();
fopts.AddArguments("--headless");
firefoxDriver = new FirefoxDriver(fopts);
firefoxDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(3);
firefoxDriver.Navigate().GoToUrl(url);
firefoxDriver.FindElementByXPath("//input[@id='search_form_input_homepage']")
driver = BrowserDriver.GetDriver("firefox", true);
driver.Navigate().GoToUrl(url);
driver.FindElementByXPath("//input[@id='search_form_input_homepage']")
.SendKeys("frankenberries" + Keys.Enter);
RemoteWebElement results =
(RemoteWebElement)firefoxDriver.FindElementByPartialLinkText("Monster cereal");
(RemoteWebElement)driver.FindElementByPartialLinkText("Monster cereal");
Assert.NotNull(results);
firefoxDriver.Quit();
driver.Quit();
}
}
}

4
weehawken/README.md Normal file
View File

@ -0,0 +1,4 @@
# WEEHAWKEN C# TEST FRAMEWORK
This framework is basically a clone port of my java framework. It's a work in progress.

View File

@ -10,8 +10,8 @@
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
<PackageReference Include="Selenium.Support" Version="3.141.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.0.0-alpha05" />
<PackageReference Include="Selenium.Support" Version="4.0.0-alpha05" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="86.0.4240.2200" />
<PackageReference Include="Selenium.WebDriver.IEDriver" Version="3.150.1.2" />
<PackageReference Include="Selenium.WebDriver.GeckoDriver" Version="0.27.0" />