Selenium Webdriver C# - Stop browser opening on program start up - performance

I am writing a program for automation testing and it works fine. I do however have an issue with the amount of time it takes from the program to start up. This is down to the face that I initialize the IWebdriver = new firefoxDriver() in the public partial class so to allow for all functions to access the driver class with ease and no fuss.
So when i load the program the browser loads which takes maybe 15/20 seconds followed by the GUI I have built. Does anybody know a way of making the "driver" global but not initializing the browser until I call it in a function? i.e. I can load my program and fiddle with the variables etc and then when I am ready I click a button, then the browser loads and executes the function all without have the Iwebdriver = new firefox() in each function separately. Also the reason I have coded it this way (making it global) was due to different browser session issues. It would not see other browsers outside of the initial one on start up
Here is the basic code I am working with
public partial class Main : Form
{
IWebDriver driver = new FirefoxDriver();
public Main()
{
InitializeComponent();
}
}

Initialize it the same way but make it static:
Public static IWebDriver Driver;
And then set it to FirefoxDriver where you need it to open the browser:
Driver = new FirefoxDriver();

Related

How to set FiddlerCore up to monitor all system traffic?

We are evaluating FiddlerCore for a use-case. Basically, for now we just want to catch all of the endpoints/urls being requested on a system. This works fine in Fiddler, no issues. But we only want to catch them while a certain vendor software is open. So we want to write a plugin to that software that will run when it launches, and then exit when it exits. Hence, using FiddlerCore (hopefully).
As proof-of-concept, I just made a simple app, one form with a textbox, that it should just append each url into the textbox. Simple as simple can be. However, it's not doing anything. I run the app, then refresh a page in my browser, and ... nothing.
Here is the entire (non-generated) code of my program...
using Fiddler;
using System;
using System.Windows.Forms;
namespace ScratchCSharp {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
FiddlerApplication.Startup(8888, FiddlerCoreStartupFlags.Default);
}
private void FiddlerApplication_AfterSessionComplete(Session s) {
textBox1.Invoke((Action)delegate () {
AddText(s.fullUrl);
});
}
public void AddText(string text) {
textBox1.Text += $"{text}\n";
}
}
}
After a little more poking around, I see that FiddlerApplication.IsSystemProxy is returning false. Seems to have to do with that the Startup flag to set as system proxy is no longer honored, and it tells you now to use the Telerik.NetworkConnections.NetworkConnectionManager to set it as the system proxy. But I can't find anywhere that actually says how to do that. The closest thing I could find is this thread which seems to be their official answer to this question. However, it only goes into a lot of talk about WHY they deprecated the flag, and what their thinking was in how they designed its replacement, but not actually into HOW TO USE the replacement. The Demo app also does NOT use these libraries (probably why it doesn't catch anything either).
The biggest problem though, is that the NetworkConnectionsManager class has no public constructor, so you can't create an instance. It is not inheritable, so you can't make a subclass instance. All of the methods on it are instance methods, not static/shared. And there seems to be no method in the libraries which will create an instance of NetworkConnectitonsManager for you.
So while the class is clearly designed to be used as an instance (hence the methods not being static/shared, there doesn't actually seem to be any way to create an instance.
Any help on how to set this thing up to catch all the outgoing URLs on the system?
You can use the following code for starting Fiddler Core and registering it as a system proxy:
FiddlerCoreStartupSettings startupSettings =
new FiddlerCoreStartupSettingsBuilder()
.ListenOnPort(fiddlerCoreListenPort)
.RegisterAsSystemProxy()
.ChainToUpstreamGateway()
.DecryptSSL()
.OptimizeThreadPool()
.Build();
FiddlerApplication.Startup(startupSettings);
Some of the methods are obsolete for now, but I would recommend to stick with them until the NetworkConnectionManager API is improved and finalized.
Also, there is a sample application (that FiddlerCore installer installs on the Desktop), which is useful for a starting point with the development.

How to start selenium with firefox driver

I try to follow this link: http://www.seleniumhq.org/docs/03_webdriver.jsp
In SetUpTest:
protected IWebDriver driver;
protected ISelenium selenium;
[SetUp]
public void SetupTest()
{
driver = new FirefoxDriver();
selenium = new DefaultSelenium(
"localhost",
4444,
"*chrome",
"http://localhost");
selenium.Start();
verificationErrors = new StringBuilder();
}
When this line driver = new FirefoxDriver(); execute, I have this error:
Additional information: Cannot find a file named '***[Link to my project]\webdriver.xpi' or an embedded resource with the id 'WebDriver.FirefoxExt.zip'.
When I change it to driver = new ChromeDriver();, it opens firefox, but it cannot find element although element already render.
How to make selenium works with firefox?
If you was using C#, this appears that you have added Nuget packages of both Selenium .NET binding and Firefox Driver
I was following this tutorial https://learn.microsoft.com/en-us/vsts/build-release/test/continuous-test-selenium#create-the-test-project. It asks to add all these packages
After the project is created, you must add the Selenium and browser driver references used by the browser to execute the tests. Open the shortcut menu for the Unit Test project and choose Manage NuGet Packages. Add the following packages to your project:
Selenium.WebDriver
Selenium.WebDriver.ChromeDriver
Selenium.WebDriver.IEDriver
Selenium.Firefox.WebDriver
Selenium.WebDriver.PhantomJS.Xplatform
This is wrong. If you are using Firefox driver, you only need Selenium.WebDriver (maybe Selenium.Support as well) and Selenium.Firefox.WebDriver. You don't need Selenium.WebDriver.PhantomJS.Xplatform which will add the wrong WebDriver.dll into your project and the test run will complain about missing .json and .xpi file.

Clear Firefox cache in Selenium IDE

I'm using Selenium IDE to test a web application. Sometimes my tests succeed even though they should have failed. The reason is that the browser happens to load a previous version of a page from the cache instead of loading the newer version of that page. In other words, I might introduce a bug to my app without being aware of it because the tests may pass after loading a previous working version instead of loading the new buggy version.
The best solution I could have thought of is to delete the browser cache before running the tests. I have a Selenium script in which I run set-up selenium commands before running the tests. Is there a selenium command to clear Firefox cache? Alternatively, is there another way to prevent loading pages from the cache during the tests?
In python this should disable firefox cache:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.cache.disk.enable", False)
profile.set_preference("browser.cache.memory.enable", False)
profile.set_preference("browser.cache.offline.enable", False)
profile.set_preference("network.http.use-cache", False)
driver = webdriver.Firefox(profile)
hope this helps someone
You can disable the cache in firefox profile.
See this link for more details.
For those programming in Java, here is how I solve the issue:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.cache.disk.enable", false);
profile.setPreference("browser.cache.memory.enable", false);
profile.setPreference("browser.cache.offline.enable", false);
profile.setPreference("network.http.use-cache", false);
FirefoxOptions options = new FirefoxOptions().setProfile(profile);
driver = new FirefoxDriver(options);
Disclaimer: I've never had to do this before (clearing the cookies has always been sufficient for me), but from what I can see, this is functionality that is lacking in the current builds of Selenium, although from recent changelogs, it looks like the developers are making a push to make a standard way of doing this. In 2.33 of iedriverserver, They have the following changenote:
Introduced ability to clean browser cache before launching IE. This version
introduces the ie.ensureCleanSession capability, which will clear the
browser cache, history, and cookies before launching IE. When using this
capability, be aware that this clears the cache for all running instances of
Internet Explorer. Using this capability while attempting to run multiple
instances of the IE driver may cause unexpected behavior. Note this also
will cause a performance drop when launching the browser, as the driver will
wait for the cache clearing process to complete before actually launching
IE
http://selenium.googlecode.com/git/cpp/iedriverserver/CHANGELOG
To do this, you would specify this at driver creation time in the DesiredCapabilities Map using ensureCleanSession.
http://code.google.com/p/selenium/wiki/DesiredCapabilities
Since you're using firefox, it looks like you're out of luck in using a native way to do this. If you haven't tried driver.manage().deleteAllCookies();, I'd try that to see if it gets you where you need to be.
For C# and Geckodriver v0.31.0
public Task<WebDriver> newInstance()
{
return Task.Run(() =>
{
foreach (var process in Process.GetProcessesByName("geckodriver"))
{
process.Kill();
}
FirefoxProfileManager profilemanager = new FirefoxProfileManager();
System.Collections.ObjectModel.ReadOnlyCollection<String> profilesList = profilemanager.ExistingProfiles;
foreach (String profileFound in profilesList)
{
Console.WriteLine(profileFound);
}
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = profilemanager.GetProfile("default");
//profile = webdriver.FirefoxProfile()
profile.SetPreference("browser.cache.disk.enable", false);
profile.SetPreference("browser.cache.memory.enable", false);
profile.SetPreference("browser.cache.offline.enable", false);
profile.SetPreference("network.http.use-cache", false);
WebDriver driver = new FirefoxDriver(options);
return driver;
});
}

How can I delete all cookies from all domains?

Webdriver Wire Protocol doesn't contain a method for deleting all cookies from all domains. It can only delete cookies from current domain.
I'd want to delete all cookies from all domains as AUT has integration with 3rd party sites that set cookies and I'd want to ensure clean state in the beginning of each test to improve ease of maintainability.
So I started to think about driver-specific ways to delete all cookies. I'm interested particularly in Firefox.
In Firefox it can be done by either:
pressing Ctrl+Shift+Delete and then Enter
writing Firefox extension that will allow to do it in one step
Do I miss something? Is there a cross-driver option to delete all cookies (from all domains)?
There are several ways to accomplish this. This is how I normally implement the work in my frameworks.
When creating a new driver object (in this case ChromerDriver) set the ENSURING_CLEAN_SESSION capability:
public WebDriver driver() {
File driverServer = new File(WebDriverConfig.class.getClassLoader().getResource("webDrivers/chromedriver.exe").getFile());
System.setProperty("webdriver.chrome.driver", driverServer.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
return new ChromeDriver();
}
Then at the beginning of each of my tests using TestNG's framework:
#BeforeMethod(alwaysRun = true)
public void setup() {
driver.manage().deleteAllCookies();
// Do other stuff before each test executes
}
You can also delete only specific cookies by getting the cookies and finding the one you want and then removing that one.
driver.manage().getCookies();
I hope this helps you in finding a resolution to your issue.

Selenium: Testing interaction between users on different browsers

I am implementing a suite to test the behavior of a chat. Each user performs different actions to log in into the chat on different browsers.
I have implemented each test case separately and work fine. I also implemented one suite which includes both cases, one first and then the other and it runs but it looks like both browsers are not sharing the information, because it does not show the user logged in. Here is an example of that I am implementing in java:
public class connect_facebook extends SeleneseTestCase{
Selenium sele1 = null;
Selenium sele2 = null;
#Before
public void setUp() throws Exception {
//Establish the first browser
sele1 = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.es/");
//Establish the second browser
sele2 = new DefaultSelenium("localhost", 4444, "*googlechrome " , "http://www.facebook.com");
//Start the first test case
sele1.start();
//Start the second test case
sele2.start();
}
//Send the message to a friend in Facebook
#Test
public void testConnect_facebook_nocookies() throws Exception {
...
}
I don't stop sele1
//Open Facebook, check the message and open Zaraproxy to start Connect
#Test
public void testCheck_mail_facebook() throws Exception {
...
}
NOTE: What it does is at the very beginning opens FF and Chrome, then closes Chrome and runs sele1. After that does not close FF but opens another FF and Chome browser, closes FF and then runs sele2 with Chrome.
When sele2 opens the chat panel, the user of sele1 (FF) appears offline.
I am using selenium server as a HUB and started another server as a node (not using selenium grid). Want to resolve with selenium RC first before passing to selenium Grid because installation of the Grid have been given me some problems.
Any help is welcome! Thanks in advance!
B

Resources