Skip to content

Instantly share code, notes, and snippets.

@xAaeEx
Last active October 30, 2018 14:31
Show Gist options
  • Save xAaeEx/2da70dbdf6a55aeebd6ca7ba253341e0 to your computer and use it in GitHub Desktop.
Save xAaeEx/2da70dbdf6a55aeebd6ca7ba253341e0 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
namespace mstest
{
[TestClass]
public class UnitTest1
{
private TestContext testContextInstance;
/// <summary>
/// Gets or sets the test context which provides
/// information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get { return testContextInstance; }
set { testContextInstance = value; }
}
[TestMethod]
//[DataRow("chrome")]
[DataRow("firefox")]
public void GoogleSeleniumHq(string browser)
{
var driver = default(IWebDriver);
Stopwatch stopWatch = new Stopwatch();
try
{
if (browser == "chrome")
{
var chromeOptions = new ChromeOptions();
// chromeOptions.AddArgument("--incognito");
// chromeOptions.AddArgument("--headless");
// chromeOptions.AddArgument("--disable-gpu");
var executableUserDirectory = @"c:\Tools\sel-drivers\chrome\";
// Need to set this because of find elements
driver = new ChromeDriver(executableUserDirectory, chromeOptions);
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
}
if (browser == "firefox")
{
var firefoxOptions = new FirefoxOptions();
firefoxOptions.SetPreference("browser.privatebrowsing.autostart", true);
//firefoxOptions.AddArgument("-headless");
var executableUserDirectory = @"c:\Tools\sel-drivers\gecko\";
driver = new FirefoxDriver(executableUserDirectory, firefoxOptions);
// Already tried with 30
// Already tried with 1
// Already tried without timeouts
// driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(1);
// driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1);
// driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(1);
}
driver.Manage().Window.Maximize();
stopWatch.Start();
driver.Navigate().GoToUrl("https://www.google.com/");
var searchBoxElement = driver.FindElement(By.Id("lst-ib"));
searchBoxElement.Click();
searchBoxElement.SendKeys("seleniumhq");
searchBoxElement.SendKeys(Keys.Enter);
var searchElements = driver.FindElement(By.Id("search"));
var clickableElements = searchElements.FindElements(By.ClassName("bkWMgd"));
Assert.IsTrue(clickableElements.Count > 0);
var firstEntryElement = clickableElements.FirstOrDefault();
var seleniumHqLink = firstEntryElement.FindElement(By.TagName("a"));
seleniumHqLink.Click();
Assert.AreEqual("Selenium - Web Browser Automation", driver.Title, driver.Title);
stopWatch.Stop();
File.WriteAllText(@"c:\Users\Public\Documents\tmp.txt", stopWatch.Elapsed.ToString());
}
finally
{
driver?.Close();
driver?.Quit();
}
}
}
}
import unittest
import os
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
import time
class Test(unittest.TestCase):
def __init__(self, testName, browser):
super(Test, self).__init__(testName)
self.browser = browser
def setUp(self):
if self.browser == "chrome":
chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument("--incognito")
# chrome_options.add_argument("--headless");
# chrome_options.add_argument("--disable-gpu");
chromeDriverPath = "c:\Tools\sel-drivers\chrome\chromedriver.exe"
self.driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chromeDriverPath)
elif self.browser == "firefox":
options = Options()
# options.headless = True
options.set_preference("browser.privatebrowsing.autostart", True)
geckoDriverPath = "c:\Tools\sel-drivers\gecko\geckodriver.exe"
self.driver = webdriver.Firefox(options=options, executable_path=geckoDriverPath)
def google_selenium_hq(self):
"""Google for \"seleniumhq\" find the first link and click it. Verify that we are on the right page. """
driver = self.driver
driver.get("https://www.google.com")
driver.set_page_load_timeout(30)
driver.implicitly_wait(30)
driver.maximize_window()
start = time.time()
driver.get("https://www.google.com/")
searchBoxElement = driver.find_element_by_id("lst-ib")
searchBoxElement.click()
searchBoxElement.send_keys("seleniumhq")
searchBoxElement.send_keys(Keys.ENTER)
clickableElements = driver.find_elements_by_class_name("bkWMgd")
assert clickableElements.__len__() > 0
seleniumHqLink = clickableElements[0].find_element_by_tag_name("a")
seleniumHqLink.click()
assert driver.title == "Selenium - Web Browser Automation"
end = time.time()
print("PureTest ellapsed: " + str(end - start))
def tearDown(self):
self.driver.close()
self.driver.quit()
if __name__ == "__main__":
suite = unittest.TestSuite()
# suite.addTest(Test("google_selenium_hq", "chrome"))
suite.addTest(Test("google_selenium_hq", "firefox"))
unittest.TextTestRunner(verbosity=2).run(suite)
@xAaeEx
Copy link
Author

xAaeEx commented Oct 30, 2018

Requirements for Python:

Python 3.6.4

  • unittest
  • selenium (3.14.1)

Nuget packages for C#:

dotnet Core 2.1 (2.1.403)

  • Microsoft.NET.Test.Sdk (15.8.0)
  • MSTest.TestAdapter (1.3.2)
  • MSTest.TestFramework (1.3.2)
  • Selenium.WebDriver (3.14.0)

WebDrivers

geckodriver v0.23.0

chromedriver v2.43

gecko: Place it on C:\Tools\sel-drivers\gecko\geckodriver.exe

chrome: Place it on C:\Tools\sel-drivers\chrome\chromedriver.exe

Run

python

python <nameOfThePythonFile>

dotnet

dotnet test <nameOfTheProjectContainingTheTestFile>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment