Clearing cache with Selenium ChromeDriver ChromeOptions [duplicate] - selenium

This question already has answers here:
How to clear browser cache in Selenium test
(3 answers)
Closed 5 years ago.
Currently using var driver = new ChromeDriver(TestContext.CurrentContext.TestDirectory);
We want to replace it as if we did:
With something like:
var options = new ChromeOptions();
options.AggressiveCacheDiscard = true; // how??
options.ChromeDriverDirectory = TestContext.CurrentContext.TestDirectory; // how??
var driver = new ChromeDriver(options);
But how do we do that? The goal being to set options that will ensure the cache/cookies are always cleared completely (From the beginning of time, as Chrome says) and that the directory is set.

There isn't a direct way to do this through Selenium. If you are trying to make sure everything is cleared at the beginning of starting a Chrome driver, or when you are done, then you don't need to do anything. Every time you initialize a webdriver, it is a brand new instance with no cache, cookies, or history. Every time you terminate the driver, all these are cleared.

Related

RemoteWebDriver - changing window size

I'm writing selenium tests. I have problem with one script because selenium can't find a element. I think that is a problem with RemoteWebDriver because when I'm watching screens I see that screen is incomplete, without right side with that button. If I'm using local driver I have line driver.manage().window().maximize() and that works. So my question is, is that possible to maximize size of RemoteWebDriver window? Maybe there is a alternative to get full screen in that Driver? I'm using Jenkins with selenium.
I have had this problem in chrome many times, and the workaround for me is to use javascript's scrollIntoView to move the viewport to that element.
In php / phpunit-selenium:
$this->execute([
'script' => 'var elm = document.getElementById("id");elm.scrollIntoView(true);',
'args' => $args
]);
// Continue to access element
You should be able to extract that bit within 'script' and execute it as raw javascript from within whatever language you use for your selenium tests.
for chrome we can use
DesiredCapabilities cap = new DesiredCapabilities();
ChromeOptions options = new ChromeOptions();
cap.setCapability(ChromeOptions.CAPABILITY, options);
options.addArguments("--start-maximized");
driver = new ChromeDriver(cap);
firefox doesnt need any driver.window().manage().maximize() in the latest geckodriver version

disable screenshots in phantomjs

How do I stop phantomjs from taking screenshots? I need to run a test in parrallel a bunch of times using jmeter. JMeter randomly fails on some of the tests and I'm suspecting it could be Phantomjs trying to save a screenshot of the test run. With multiple threads trying to write a file to the same place at the same time I'm assuming this may be an issue. If not I'd still like to try disabling the ability to save a screenshot.
I had needed to disable phantomjs from logging and had gotten it to work with the following code:
headlessNoLogs {
driver = {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("takesScreenshot", false);
ArrayList<String> cliArgsCap = new ArrayList<String>();
cliArgsCap.add("--webdriver-loglevel=NONE");
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
Logger.getLogger(PhantomJSDriverService.class.getName()).setLevel(Level.OFF);
new PhantomJSDriver(caps)
}
}
Disabling the logging is working as expected. But I'm setting takesScreenshot, false and phantomjs is still taking screenshots.

Multiple Browser WebDriver Selenium

I am working on a Selenium test project where I need to launch two browsers at initial setup .
Then I need to do switching between these browsers.
So I will have [Window1] [Window2]
I would like to run test through [Window1] and then switch to [Window2] to check result of actions done in [Window1]
Any idea on how to do it?
I tried driver.switchTo().window() but no luck.
Any help would be greatly appreciated. Thanks
driver.switchTo().window() will work only if new window is opened by any action in existing window. If you are using different drivers to open different windows then it wont work.
In such case you need to choose appropriate instance of driver to control the new window.
Suppose you have instance of webdriver
// Window 1
WebDriver chrome = new ChromeDriver()
// Window 2
WebDriver firefox = new FirefoxDriver()
Now use chrome whenever you want to interact with Window 1 and use firefox to interact with Window 2.
Just use two driver instancess:
WebDriver driver1 = new ChromeDriver()
WebDriver driver2 = new FirefoxDriver()
You can make them both same flavour if you want.
You need to pass the parameter as window name or you can get all the window handles and then switch to the particular window handle.
You could use:
driver.switchTo().window("windowName");
or:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}

Selenium: Can't SendKeys() to an item that was below the visible window but was made visible by Click()

I have this problem with a text field that is visible at the time of the SendKeys. I'm using IEDriverServer.exe and C#.
Here's how I can reproduce the problem:
The text field in question is visible in the window but you have to scroll down to see it. To scroll down I click on the element using code like this:
var element = driver.FindElement(By.Xpath("…"));
element.Click();
This scrolls the window down and makes the text field visible.
But when I try to send text to now-visible window:
element.SendKeys("blah");
I get the exception:
When_applicant_enters_application.Should_be_instantly_approved_on_external threw exception: OpenQA.Selenium.ElementNotVisibleException: Element is not displayed
How can I fix or workaround this problem?
Selenium version: 2.32.1
OS: Windows 7
Browser: IE
Browser version: 9.0.15
I've written code demonstrating the problem and submitted it to the Selenium tech support volunteers.
The full discussion is at http://code.google.com/p/selenium/issues/detail?id=5620
but the take-home is:
// Doesn't work
// driver = new InternetExplorerDriver();
// driver.Navigate().GoToUrl(#"D:\CGY\selenium\Bug5620\Bug5620\Bug5620.htm");
// Works
// driver = new FirefoxDriver();
// driver.Navigate().GoToUrl(#"D:\CGY\selenium\Bug5620\Bug5620\Bug5620.htm");
// Works
driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(#"http://localhost:8080/Bug5620/"); // Hosted on Tomcat
so there may be a problem that possibly involves IE, IE security settings, Visual Studio local servers and/or the IE Driver. This may not even be a code problem, but something that needs to be documented, since other people are apparently running into the problem.
I don't know where the problem is exactly but I do have a work-around at this point, which is to use the Firefox Driver.
Thanks for your help, Jim. If you find out a better way of dealing with the problem, please add an answer here for the other folks.

Login popup window using selenium webdriver?

the popup window is only happening if I use the Fire Fox browser otherwise, is there a way to fix this problem? I have to enter userid/password every time the i use FF as my browser.
currently, I am entering every time i run my test which is very painful but looking to make it more automated....
I have goggled and found two links here and here but no avail
http://username:password#xyz.com
This worked for me (xyz.com being the site name)
After spending hours reading I finally found the solution which works pretty well and I hope this will help others too. - Enjoy!!
First - follow this steps:
1) Open the FireFox browser
2) Type the following `about:config`
3) Look for `network.http.phishy-userpass-length` if you don't find then create a new Integer key
Create a new Integer key (right-click->New->Integer): `network.http.phishy-userpass-length` with value `255`
Second: You need to create a Firefox driver with the following:
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("network.http.phishy-userpass-length", 255);
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "YOUR HOST ADDRESS HERE");
_driver = new FirefoxDriver(profile);
let me know if you have any questions.
If this is a windows user account & password, then you need to enable the integrated windows login by setting
network.negotiate-auth.delegation-uris: MyIISServer.domain.com
network.automatic-ntlm-auth.trusted-uris: MyIISServer.domain.com
network.automatic-ntlm-auth.allow-proxies: True
network.negotiate-auth.allow-proxies: True
in the Firefox profile that WebDriver starts. Once you have the profile created and saved (run "Firefox -P" when no other instances are running to select a profile), you can do this in the code:
File profileDir = new File("C:/wherever/SeleniumFirefoxProfile");
FirefoxProfile profile = new FirefoxProfile(profileDir);
profile.setEnableNativeEvents(true);
driver = new FirefoxDriver(profile);
I have had to handle these a few times, but my approach is using a script outside Selenium. Are you working on Windows?
Basically what you do is this:
1) Prior to loading the page, clicking the URL, etc that causes that dialog to appear:
-- Launch an asynchronous script to handle the login
2) Then load the page, click the link, etc
-- Selenium will block until your asynch script completes
The async script:
-- Sleep for a few seconds
-- Activate the dialog
-- Send the username
-- Send a TAB
-- Send the password
-- Send a TAB
-- Send the Enter Key
If you are working on windows, I can post sample scripts to handle this. I've done it with Java and C#, but I would guess that basically the same thing would work regardless of how you are writing your tests (unless you are strictly using the FF plugin, in which case this won't work).
Let me know if you'd like more details.
You can use a FF plugin "autoauth". Download this plugin and create Firefox instance by the following way:
FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
File pluginAutoAuth = new File("D:\\autoauth-2.1-fx+fn.xpi");
firefoxProfile.addExtension(pluginAutoAuth);
driver = new FirefoxDriver(firefoxProfile);
I used "autoauth-2.1-fx+fn.xpi"