Here is my sample code which I tried but I cannot get the tunnelidentifier set up and launch test url. Other than generic sites which are not blocked by proxy. Any help is appreciated. Thanks
[Test]
public void SimpleTest1()
{
Uri sauceHubURL = new Uri("https://USERNAME:SOMEACCESSKEY#123.saucelabs.com:443/wd/hub");
InternetExplorerOptions options = new InternetExplorerOptions();
options.AddAdditionalCapability("parentTunnel", "PQSauceLabs");
options.AddAdditionalCapability("tunnelIdentifier", "Mys-sauce-Tests");
options.AddAdditionalCapability("EnsureCleanSession", true);
string url = "https://www.google.com";
string companyurl = "https://www.sample.com";
var remoteDriver = new RemoteWebDriver(sauceHubURL, options.ToCapabilities(), TimeSpan.FromSeconds(300));
remoteDriver.Navigate().GoToUrl(url);
//remoteDriver.SwitchTo().Alert().SetAuthenticationCredentials("q25215", "test#");
//IAlert alert = remoteDriver.SwitchTo().Alert();
//alert.SendKeys("q25215"+ Keys.Tab + "test#" + Keys.Tab);
//alert.Accept();
string title = remoteDriver.Title;
Console.WriteLine("Getting the page title: " + title);
NUnit.Framework.Assert.AreEqual(title, "Google", "Compared values not equal");
remoteDriver.Quit();
}
I'm guessing this was run using the W3C Selenium bindings, so the sauce-specific options like parentTunnel need to be namespaced under sauce:options:
var driverOptions = new FirefoxOptions();
driverOptions.PlatformName = "macOS 10.13";
var sauceOptions = new Dictionary<string, object>();
sauceOptions.Add("username", sauceUserName);
sauceOptions.Add("accessKey", sauceAccessKey);
sauceOptions.Add("name", TestContext.CurrentContext.Test.Name);
sauceOptions.Add("parentTunnel", "Mys-sauce-Tests");
sauceOptions.Add("tunnelIdentifier", "PQSauceLabs");
driverOptions.AddAdditionalOption("sauce:options", sauceOptions);
//create a new Remote driver that will allow your test to send
//commands to the Sauce Labs grid so that Sauce can execute your tests
_driver = new RemoteWebDriver(new Uri("http://ondemand.saucelabs.com:80/wd/hub"),
driverOptions);
Related
I'm trying to intercept URLs containing a substring in C# using selenium chrome webdriver 4.0.0-beta4.
This is what I found and changed a little bit:
using V89 = OpenQA.Selenium.DevTools.V89;
using V89Net = OpenQA.Selenium.DevTools.V89.Network;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools;
ChromeOptions options = new ChromeOptions();
ChromeDriver webDriver;
IDevTools devTools;
public void InterceptRequestWithFetch(string url)
{
options.BinaryLocation = #"C:\Program Files\Google\Chrome Beta\Application\chrome.exe";
var service = ChromeDriverService.CreateDefaultService();
service.LogPath = AppDomain.CurrentDomain.BaseDirectory + "chromedriver.log";
service.EnableVerboseLogging = true;
webDriver = new ChromeDriver(service, options);
devTools = webDriver as IDevTools;
var devToolsSession = devTools.CreateDevToolsSession();
var fetch = devToolsSession.GetVersionSpecificDomains<V89.DevToolsSessionDomains>().Fetch;
var enableCommandSettings = new V89.Fetch.EnableCommandSettings();
var requestPattern = new V89.Fetch.RequestPattern();
requestPattern.RequestStage = V89.Fetch.RequestStage.Response;
requestPattern.ResourceType = V89Net.ResourceType.XHR;
requestPattern.UrlPattern = "*://*/*.jpg*";
enableCommandSettings.Patterns = new V89.Fetch.RequestPattern[] { requestPattern };
fetch.Enable(enableCommandSettings);
fetch.RequestPaused += RequestIntercepted;
webDriver.Navigate().GoToUrl(url);
}
void RequestIntercepted(object sender, V89.Fetch.RequestPausedEventArgs e)
{
richTextBox1.AppendText(e.Request.Url);
webDriver.Quit();
}
The problem is CreateDevToolsSession() does not exists and it seems like GetDevToolsSession() is the only option which does totally different job, but I tried it anyway and then my form froze, and codes past that line never executed.
I searched last three days for a solution but its just CreateDevToolsSession(). How can I use the DevTools if I won't be able to create a session?
This worked for me. Might not be exactly what you want, but it sets up devtools and can do whatever you normally would.
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.DevTools.V96.Network;
using DevToolsSessionDomains = OpenQA.Selenium.DevTools.V96.DevToolsSessionDomains;
public void DevtoolsExample()
{
IDevToolsSession session;
DevToolsSessionDomains devToolsSession;
//Setup WebDriver and devtools
driver = new ChromeDriver();
var baseUrl = ConfigurationHelper.Get<string>("TargetUrl");
//*this appears to create devtools session or get existing
IDevTools devTools = driver as IDevTools;
session = devTools.GetDevToolsSession();
devToolsSession = session.GetVersionSpecificDomains<DevToolsSessionDomains>();
devToolsSession.Network.Enable(new EnableCommandSettings());
devToolsSession.Network.SetBlockedURLs(new SetBlockedURLsCommandSettings()
{
Urls = new string[] { "*://*/*.css", "*://*/*.jpg", "*://*/*.png" }
//Urls = new string[] { }
});
driver.Navigate().GoToUrl("https://someUrl.com");
}
I am writing some simple C# code to try automatically getting HAR file from Chrome browser. I am using browser-mob-proxy and there is a function: GetHar() which is supposed to return some different entries of URL, request and response time, etc. However, it always return me only 1 entry which is the original URL I am negativing to: www.google.com
I've tried to use dr.Navigate().Refresh() to make sure the page is reloaded so there are some activities on chrome DevTool Network section.
server.Start();
Thread.Sleep(1000);
Client client = server.CreateProxy();
client.NewHar("google");
var chromeOptions = new ChromeOptions();
var seleniumProxy = new Proxy { HttpProxy = client.SeleniumProxy };
chromeOptions.Proxy = seleniumProxy;
var dr = new ChromeDriver(chromeOptions);
dr.Navigate().GoToUrl("http://www.google.com");
dr.FindElementByClassName("gb_e").Click();
Thread.Sleep(3500);
dr.Navigate().Refresh();
// Get the performance stats
HarResult harData = client.GetHar();
Log log = harData.Log;
Entry[] entries = log.Entries;
foreach (var e in entries)
{
Request request = e.Request;
Response response = e.Response;
var url = request.Url;
var time = e.Time;
var status = response.Status;
var testStr = "Url: " + url + " - Time: " + time + " Response: " + status;
}
I expected GetHar() function will return more entries instead of only 1.
Not sure why, but the issue has been resolved by adding SSL proxy:
var seleniumProxy = new Proxy { HttpProxy = client.SeleniumProxy , SslProxy = client.SeleniumProxy };
I am trying to upgrade from Selenium 2 to Selenium 3 but the old handling, which was pretty easy and fast doesn't work anymore (and the documentation is nonexisting as it seems)
This is the program at the moment and what I want is to open a Firefox driver with the profile: SELENIUM
Sadly it doesn't work and always shuts down with the Error:
An unhandled exception of type 'System.InvalidOperationException' > occurred in WebDriver.dll
Additional information: corrupt deflate stream
This is my program at the moment:
public Program()
{
FirefoxOptions _options = new FirefoxOptions();
FirefoxProfileManager _profileIni = new FirefoxProfileManager();
FirefoxDriverService _service = FirefoxDriverService.CreateDefaultService(#"C:\Programme\IMaT\Output\Release\Bin");
_service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
try
{
if ((_options.Profile = _profileIni.GetProfile("SELENIUM")) == null)
{
Console.WriteLine("SELENIUM PROFILE NOT FOUND");
_profile.SetPreference("network.proxy.type", 0); // disable proxy
_profile = new FirefoxProfile();
}
}
catch
{
throw new Exception("Firefox needs a Profile with \"SELENIUM\"");
}
IWebDriver driver = new FirefoxDriver(_service,_options,new System.TimeSpan(0,0,30));
driver.Navigate().GoToUrl("ld-hybrid.fronius.com");
Console.Write("rtest");
}
static void Main(string[] args)
{
new Program();
}
Without Loading the Profile it works with just new FirefoxDriver(_service) but the profile is mandatory.
In Selenium 2 I handled it with this code:
FirefoxProfileManager _profileIni = new FirefoxProfileManager();
// use custom temporary profile
try {
if ((_profile = _profileIni.GetProfile("SELENIUM")) == null)
{
Console.WriteLine("SELENIUM PROFILE NOT FOUND");
_profile.SetPreference("network.proxy.type", 0); // disable proxy
_profile = new FirefoxProfile();
}
}
catch
{
throw new Exception("Firefox needs a Profile with \"SELENIUM\"");
}
_profile.SetPreference("intl.accept_languages", _languageConfig);
_driver = new FirefoxDriver(_profile);
Fast and simple, but as the Driver doesn't support a Constructor with service and profile I don't really know how to get this to work, any help would be appreciated
This exception is due to a bug in the .Net library. The code generating the Zip of the profile is failing to provide a proper Zip.
One way to overcome this issue would be to overload FirefoxOptions and use the archiver from .Net framework (System.IO.Compression.ZipArchive) instead of the faulty ZipStorer:
var options = new FirefoxOptionsEx();
options.Profile = #"C:\Users\...\AppData\Roaming\Mozilla\Firefox\Profiles\ez3krw80.Selenium";
options.SetPreference("network.proxy.type", 0);
var service = FirefoxDriverService.CreateDefaultService(#"C:\downloads", "geckodriver.exe");
var driver = new FirefoxDriver(service, options, TimeSpan.FromMinutes(1));
class FirefoxOptionsEx : FirefoxOptions {
public new string Profile { get; set; }
public override ICapabilities ToCapabilities() {
var capabilities = (DesiredCapabilities)base.ToCapabilities();
var options = (IDictionary)capabilities.GetCapability("moz:firefoxOptions");
var mstream = new MemoryStream();
using (var archive = new ZipArchive(mstream, ZipArchiveMode.Create, true)) {
foreach (string file in Directory.EnumerateFiles(Profile, "*", SearchOption.AllDirectories)) {
string name = file.Substring(Profile.Length + 1).Replace('\\', '/');
if (name != "parent.lock") {
using (Stream src = File.OpenRead(file), dest = archive.CreateEntry(name).Open())
src.CopyTo(dest);
}
}
}
options["profile"] = Convert.ToBase64String(mstream.GetBuffer(), 0, (int)mstream.Length);
return capabilities;
}
}
And to get the directory for a profile by name:
var manager = new FirefoxProfileManager();
var profiles = (Dictionary<string, string>)manager.GetType()
.GetField("profiles", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(manager);
string directory;
if (profiles.TryGetValue("Selenium", out directory))
options.Profile = directory;
Trying to set a Default language in my program. But after much googling i dident find any answers.
How can i input standard english accept language?
is there any document i can read for all functions i can use in c# with PhantomJS? I dident find anything more then playing in Visualstudio controlls.
var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
driverService.LoadImages = false;
driverService.SslProtocol = "tlsv1";
driverService.IgnoreSslErrors = true;
driverService.ProxyType = "http";
driverService.Proxy = "";
using (var driver = new PhantomJSDriver(driverService))
{
driver.Manage().Window.Size = new Size(1920, 1080); // Size is a type in assembly "System.Drawing"
driver.Manage().Cookies.DeleteAllCookies();
driver.Url = "https://www.thewebsite.com";
Thread.Sleep(5000); // 5sec
try
{
driver.FindElement(By.Name("email")).SendKeys("MyEmail");
driver.FindElement(By.Name("fullName")).SendKeys("MyName");
driver.FindElement(By.Name("username")).SendKeys("MyUsername");
driver.FindElement(By.Name("password")).SendKeys("MyPassword");
driver.TakeScreenshot().SaveAsFile("LetsSnapascreenshot.png", ImageFormat.Png);
}
catch (OpenQA.Selenium.NoSuchElementException exception)
{
var exmsg = exception;
driver.TakeScreenshot().SaveAsFile("Snaperrorscreenshot.png", ImageFormat.Png);
}
}
I found the solution for this if anyone else is looking for it.
PhantomJSOptions options = new PhantomJSOptions();
options.AddAdditionalCapability("phantomjs.page.customHeaders.Accept-Language", "en,en;q=0.5");
IWebDriver driver = new PhantomJSDriver(options);
And now it outputs the accept-Language correct
you can try to add a header
PhantomJSOptions options = new PhantomJSOptions();
options.AddAdditionalCapability("Accept-Language","en-DE,en;q=0.5");
IWebDriver driver = new PhantomJSDriver(options);
or write in your index.html as described in this answer
<script>
localStorage.lang = 'en';
</script>
I use Selenium WebDriver in my C# winforms application. i have a problem in aoutologin.
After opening Firefox pop up Alert with input fields username and password.
var profile = new FirefoxProfile();
profile.SetPreference("general.useragent.override", [UserAgent]);
Proxy proxy = new Proxy();
proxy.HttpProxy = proxy;
proxy.FtpProxy = proxy;
proxy.SslProxy = proxy;
proxy.SocksProxy = proxy;
proxy.SocksUserName = username;
proxy.SocksPassword = password;
profile.SetProxyPreferences(proxy);
profile.SetPreference("network.websocket.enabled", false);
IWebDriver driver = new FirefoxDriver(profile);
driver.Url = siteUrl;
This is an old thread, but this is how I made it to work:
public static bool InitializeAndSetupBrowser(string proxyIp, string proxyUsername, string proxyPassword, string proxyPort)
{
try
{
var proxy = new
{
Ip = proxyIp,
Username = proxyUsername,
Password = proxyPassword,
Port = proxyPort
};
string PROXY = proxy.Ip + ":" + proxy.Port;
Proxy pro = new Proxy();
pro.HttpProxy = PROXY;
pro.FtpProxy = PROXY;
pro.SslProxy = PROXY;
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.Proxy = pro;
PropertiesCollection.Driver = new FirefoxDriver(firefoxOptions);
Navigate(""); //this method is my internal method, just navigate in to page, this makes the proxy credentials dialog to appear
try
{
WebDriverWait wait = new WebDriverWait(PropertiesCollection.Driver, TimeSpan.FromSeconds(15));
wait.Until(ExpectedConditions.AlertIsPresent());
IAlert alert = PropertiesCollection.Driver.SwitchTo().Alert();
alert.SendKeys(proxy.Username + Keys.Tab + proxy.Password);
alert.Accept();
}
catch { }
return true;
}
catch (Exception exc)
{
Logger.Log("Could not start browser.", exc);
return false;
}
}
You will need to use an AutoIT script.
WinWait("Authentication Required","","20")
If WinExists("Authentication Required") Then
WinActivate("Authentication Required")
Send($CmdLine[1])
Send("{TAB}")
Send($CmdLine[2])
Send("{ENTER}")
EndIf
I am also working on another solution to use firefox about:cnofig properties to solve this issue. Will let you know if I come up with something.
Try below code to set the proxy first and then autologin.
firefoxProfile.SetPreference("network.proxy.type", 1);
firefoxProfile.SetPreference("network.proxy.http", "add server name");
firefoxProfile.SetPreference("network.proxy.http_port", 8080);
firefoxProfile.SetPreference("network.proxy.ssl", "add server name");
firefoxProfile.SetPreference("network.proxy.ssl_port", 8080);
firefoxProfile.SetPreference("network.proxy.no_proxies_on", "add website url(s)");
Driver = new FirefoxDriver(firefoxProfile);
Hope this will work for you.
Thanks,
Anshul
String PROXY = "http://login:pass#proxy:port";
ChromeOptions options = new ChromeOptions();
options.AddArguments("user-data-dir=path/in/your/system");
Proxy proxy = new Proxy();
proxy.HttpProxy = PROXY;
proxy.SslProxy = PROXY;
proxy.FtpProxy = PROXY;
options.Proxy = proxy;
// Initialize the Chrome Driver
using (var driver = new ChromeDriver(options))