Login popup window using selenium webdriver? - selenium

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"

Related

How Can I set chrome browser to automatically download a pdf using QAF and WebDriverManager

Using a datasheet, I usually pass a browser name into a class I created to select which browser I want to run my tests from. Recently, I've been working on an app in which I need to download a PDF and then verify its contents. I have everything working successfully other than downloading the PDF. With WebDriverManager, it creates a browser profile every time a test runs, and so, I need to update chromeOptions to download PDFs automatically before at the start of the script.
Here is the code that I already have. I just need help with what to put in the prefs for this to work. -
public static void selectBrowser(String strBrowser) {
switch (strBrowser) {
case "Chrome":
String chromePrefs = "{'goog:chromeOptions':{'prefs':{'profile.default_content_settings.popups':0}}}";
ConfigurationManager.getBundle().setProperty("chrome.additional.capabilities", chromePrefs);
TestBaseProvider.instance().get().setDriver("chromeDriver");
Reporter.log("Chrome Browser was set", MessageTypes.Info);
break;
}
}
After researching for hours, finally found that I needed to use the plugins.always_open_pdf_externally preference. Here is the code for anyone who might need it.
Note, the "goog:" before chromeOptions is necessary since I have WebDriverManager enabled. With 3rd party driver managers, we need to put "goog:" before chromeOptions for it to work.
You can simply put it in the application.properties like this -
chrome.additional.capabilities={"goog:chromeOptions":{"args":[--disable-extensions],"prefs":{"plugins.always_open_pdf_externally":true}}}
or you can put it in the code I have up top like this
String chromePrefs = "{'goog:chromeOptions':{'args':[],'prefs':{\"plugins.always_open_pdf_externally\":true}}}";

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

How to automate a textbox where area code of phone number gets added automatically?

Scenario is with an eComm website:
Cases are :
Open https://www.flipkart.com/
Click on Login URL
Top Window opens up.
Try adding phone number [Don't login using eMail ID]
Find a +91 gets added as a prefix automatically whenever you add a phone number.
I am using Selenium WebDriver and Java for automating this scenario. Any help would be appreciated.
Thanks.
Nothing special for this specific case here. Just open up the login window and use the code below:
driver.findElement(By.cssSelector("input[class='_2zrpKA']")).sendKeys("Your phone number goes here");
Somehow we are unable to get Dynamic Xpaths.
Try this absolute path
driver.findElement(By.xpath("html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input")).sendKeys("1231");
There's a bug in Firefox WebDriver and now it got fixed.
Download the latest Gecko Driver from here
Closing this. It now works for me with the configuration FF 53, geckodriver-v0.16.1-win64, Windows 64 Bit and Selenium 3.4.0
More can be checked here : https://github.com/mozilla/geckodriver/issues/659
WebElement phone = driver.findElement(By.xpath("//[#id="load_form"]/fieldset[2]/input"));
phone.sendKeys("123456789");
This worked for me!!!

Issue with WebDriver test case

I have written a test case using WebDriver, I closed the browser in one method and again I am opening the browser not able to invoke
driver.close();i closed the browser through above command for again opening a browser i driver.get(url)
but i am getting error
'Error communicating with the remote browser It may have died'
If you really want to close the browser before navigating to the new URL, then do:
driver.quit();
driver = new FirefoxDriver();
driver.get(url);
However, why would you want to close the browser before navigating to the new URL in the first place?
I can think of either one of two reasons:
You want to go to the next URL with your browser's history cleaned.
If this is indeed the case, then deleteAllCookies will do the job.
You cannot go to the next URL because some popup alert is preventing you doing it.
If this is indeed the case, then neither close not quit will do the job.
Well you killed the browser with your driver.close();
In order to use the driver you must create a new one using something like driver = new FirefoxDriver(capabilities);
Moving to answer from comment.
I think I got your issue. Though you have called the close() and it only should close the current window, you should use this if you want to shift between multiple windows.
For your case just don't close the driver it will use the same window to open the url.
You have to use following way if you want to close the current browser and open
a new one:
webDriver.Close();
//Goto the Target website
WebDriver.Navigate().GoToUrl("url");
And you can use following way if you want to close the browser and kill the
web driver:
webDriver.Close();
webDriver.Quit();

How could I start a Selenium browser(like Firefox) minimized?

How could I start a Selenium browser (like Firefox) minimized? I want the command browser.start(mySettings) to start a browser minimized.
I have an alternate solution that may meet your needs. You can set the position of the WebDriver to be outside of your view. That way, it'll be out of sight while it runs. (It may start at a visible location, but it will only be there for an instant.)
FirefoxDriver driver = new FirefoxDriver();
driver.manage().window().setPosition(new Point(-2000, 0));
Your question does not say that why you want to run your test cases in minimized browser but unfortunately selenium do not provide any built-in function for the same.
Normally when we want to run test cases with maximized browser we use driver.manage().window().maximize();
No doubt there are several ways to minimize your window through code by using Java key event by using keyboard shortcuts for minimimzing window or by using JavaScriptExecuter but that too depend on which OS and language you are working.
One more thing you can try is HtmlUnitDriver.By using this you cant even see the browser, so that may also serve your purpose if you have a case of not opening the browser while execution of test cases.
Dimension windowMinSize = new Dimension(100,100);
driver.manage().window().setSize(windowMinSize);
For C# you can minimize window easily, also with a built in way.
See: https://www.selenium.dev/documentation/en/webdriver/browser_manipulation
Screenshot:
Without knowing your motive for minimizing the browser and assuming that you are using the WebDriver drivers (Selenium v2) and don't want a UI to pop up, one could use the lightweight browser HtmlUnitDriver.
After you define the driver
driver = webdriver.Chrome(r"FULL PATH TO YOUR CHROMEDRIVER")
Do
driver.minimize_window()
And then call the site
driver.get(r'SITE YOU WANT TO SELECT')
It will minimize.
When Using Python to Move the FireFox Browser off screen:
driver = webdriver.FireFox()
driver.set_window_position(-2000,0)
driver.set_window_position(2000,2000)
or
(x,y) values more than monitor resolution
Later if u want to see the window again
driver.maximize_window()
The workarounds mentioned in the post did not work for NodeWebKit browser, so as a workaround i had to use native C# code as mentioned below:
public static void MinimiseNWKBrowser(IWebDriver d)
{
var body = UICommon.GetElement(By.TagName("body"), d);
body.Click();
string alt = "%";
string space = " ";
string down = "{DOWN}";
string enter = "{ENTER}";
SendKeys.SendWait(alt + space);
for(var i = 1; i <= 5; i++)
{
SendKeys.SendWait(down);
}
SendKeys.SendWait(enter);
}
So this workaround basically uses "ALT+SPACE" to bring up the browser action menu to select "MINIMIZE" from the options and presses "ENTER"
In php we can use JavaScript command to minimize the browser window.
$this->selenium->getEval("Minimize();");
and similar command for java :
browser.getEval("Minimize();");
The best way to minimize your browser is to use shortcut using Robot class.
Shortcut: Alt+Space+N (for Windows)
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_N);
By using above code you can minimize your browser.
Selenium doesn't have a built-in method to minimize the Browsing Context. Minimizing the browser while the Test Execution is In Progress would be against the best practices as Selenium may loose the focus over the Browsing Context and an exception may raise during the Test Execution. You can find a relevant detailed discussion in How to execute tests with selenium webdriver while browser is minimized
However, to mimic the functionality of minimizing the Browsing Context you can use the following steps:
Set the dimension of the Browsing Context to [0, 0]
Set the position of the top left corner of the Browsing Context just below the Viewport
Code Block (Java):
driver.navigate().to("https://www.google.com/");
Point p = driver.manage().window().getPosition();
Dimension d = driver.manage().window().getSize();
driver.manage().window().setSize(new Dimension(0,0));
driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
You can use:
driver.manage().window().maximize();
For example code snippet with chrome driver:
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
driver = new ChromeDriver();
baseUrl = "chrome://newtab/";
driver.manage().window().maximize().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
for Firefox use just "firefordriver.exe"
driver.manage().window().minimize();
This should help minimize the window. You can also use "maximize" in place of "minimize" to maximize the window.