Selenium - Block popup using firefox driver - selenium

I want to block popup in selenium using firefox driver.
has anyone done anything like this? i have had tried by loading extension but no success.
Can anyone help here?
Thanks!
Regards,
Kalpesh Patel

You need to use your browser profile and block popup there. Pass your profile as a parameter while creating instance of your browser. check the code below:
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("Your-profile-name");
profile.setPreference( "intl.accept_languages", "no,en-us,en" );
FirefoxDriver driver = new FirefoxDriver(profile);
driver.get("http://www.example.com/");
Note: I consider that you already know the concept of profile in browser.

Related

Is it possible change browser language in firefox using selenium?

I want to launch firefox browser in a given language in selenium automation. Can I do that? If so, please share some sample code implementation.
Thanks in advance.
Set the language code in fire fox profile preference before launch the the driver as shown below.
FirefoxProfile ffprofile = new FirefoxProfile();
//For japenese language
ffprofile.setPreference("intl.accept_languages","ja");
driver = new FirefoxDriver(ffprofile);
driver.get("https://www.google.com");
For language codes : https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

How do I test a website without opening it using automation?

I am able to use Selenium web driver to test the website and get the result. But I need to get the result without opening a single web page. I made some R&D and found Selenium can't do this.
Can any body tell me how can I achieve this?
Thanks,
Rahul
Give Htmlunitdriver a try
Enter this line in your code
driver = new HtmlUnitDriver(true);
true = to enable javascript support.
In case you'll see many warnings in your console once you run your test. To ignore them simply do this
static Logger log = Logger.getLogger("com.gargoylesoftware");
log.setLevel(Level.OFF);
driver = new HtmlUnitDriver(true);
Else there is always other options like PhantomJS
Source: SoftwareAutomata

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

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);
}

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"