To run tests on different browsers using Selenium webdriver+ TestNG+Java - java

I am trying to run my tests using Selenium web driver + TestNG + Java, everything runs fine with Firefox, but when I tried to extend it to different browsers like IE and Chrome, I have included code for it, but it doesn't seem to work. I am using some excel files to pass input while automating it. Thanks in advance

Required Configuration with IE
1.The IEDriverServer exectuable must be downloaded and placed in your PATH.
2.On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings, choose "Internet Options..." from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled "Enable Protected Mode".
3.The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.
Check if java plugin is installed in your browser.
For more info go to http://code.google.com/p/selenium/wiki/InternetExplorerDriver

You cannot work with chrome/IE same as Firefox. In order to work with that you need chrome/IE driver, which is an executive file. The code for invoking the chrome and IE is a little different than Firefox.
For chrome you can take help from here
For IE you can take help from here
example for invoking driver
if(BrowserName.equalsIgnoreCase("Firefox")){
driver = new FirefoxDriver();
}else if(BrowserName.equalsIgnoreCase("Chrome")){
String ChromeDriverPath= "D:\\ChromeDriver\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", ChromeDriverPath);
driver=new ChromeDriver();
}else if(BrowserName.equalsIgnoreCase("IE")){
IEDriverPath32bit= "D:\\IEDriver\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", IEDriverPath32bit);

Related

EDGE crashes with webDriver

I’m using,
◦Selenium 3.12.0
◦Java binding
◦Windows 10 (build 15063.1029)
◦EDGE driver (Version: 4.15063 | Edge version supported: 15.15063 )
Here is my code to open the browser
import org.openqa.selenium.edge.EdgeDriver;
System.setProperty(“webdriver.edge.driver", C://mE//Selenium//Installation//Drivers//Edge//v15063//MicrosoftWebDriver.exe”);
WebDriver driver = new EdgeDriver();
The browser (EDGE) opens for a sec and then crashes immediately. Here is the error (unknown),
Exception in thread “main” org.openqa.selenium.WebDriverException: Unable to parse remote response: Unknown error Build info: version: '3.11.0’, revision: 'e59cfb3’, time: ‘2018-03-11T20:33:08.638Z’ os.name: 'Windows 10’, os.version: '10.0’, java.version: ‘10.0.1’ Driver info: driver.version: EdgeDriver at
org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:111) at
org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73) at
org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136) at
org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545) at
org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:209) at
org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:132) at
org.openqa.selenium.edge.EdgeDriver.(EdgeDriver.java:141) at
org.openqa.selenium.edge.EdgeDriver.(EdgeDriver.java:96) at
projectOne.classOne.main(classOne.java:66)
This only happens with EDGE. Other browsers (IE, Chrome & FireFox) are launching just fine. I realize that, there is a limitation that EDGE allows only one instance to be running for webDriver testing. In my case, there was no other instance of EDGE running and yet it was crashing soon after it launched. I'm wondering is there any settings changes need to be made in EDGE in order for it to work with webDriver.
There shouldn't be any other settings you need to make this work.
Sometimes this can not work if MicrosoftWebDriver.exe is stuck in a zombie-type state. So check your task manager that this isn't the case.
Your MicrosoftWebDriver.exe seems to match your windows version, but it might be worth just going to https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ to just download a fresh copy to ensure there's nothing funky going on there.
Something else to try, try launching edge via this code:
var edgeService = EdgeDriverService.CreateDefaultService();
edgeService.UseVerboseLogging = true;
var edgeOptions = new EdgeOptions();
driver = new EdgeDriver(edgeService, edgeOptions);
You might be able to get more logging out of this too.
Could be below reason
Download the Webdriver as per the OS Build Version
Check the Edge browser opening only one tab on manually opening the browser
it should have only one URL in the Browser settings --> open Microsoft edge with
Edge by dafault runs on instance to make the page load faster. This is the default setting in Windows 10. So even if we close edge one instance is always running. We can disable it.
Follow the steps here:
https://www.askvg.com/windows-10-what-are-microsoft-edge-exe-processes-running-in-task-manager-and-how-to-disable-them/
For me the following fix worked:
Go to WebDriver.
Install Microsoft Edge Driver according to the version of edge browser(You can go to this page to have reference: Use WebDriver (Chromium) for test automation).
Go to downloads and unzip the driver.
Now you can write the following code in selenium in java.
System.setProperty("webdriver.edge.driver", "C:\\Users\\User-name\\Downloads\\msedgedriver.exe");
driver = new EdgeDriver();
Please heck your browser version and then pick the right one from the below link ,
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/#downloads
This is working fine for me and i too faced the similar problem.

Open firefox devtools programmatically with FirefoxProfile

How can I launch Firefox 52 with opened devtools using selenium and Java?
Before the merge of Firebug into the Firefox DevTools I used FirefoxProfile to open the console programmatically:
profile.setPreference("extensions.firebug.console.enableSites", true);
etc.
But Firebug does not work anymore now.
So what is the proper way to launch with opened network or console tab?
And also, is there any analog extension to FirePath to launch automatically and use instead of the currently broken FirePath extension?
I managed to launch Firefox with given page and native devtools open from command line using -devtools parameter:
firefox -no-remote -profile "c:\deleteme" -devtools -url "http://example.com/"
It seems to be possible to pass that parameter to the WebDriver by calling
addCommandLineOptions("-devtools") on the FirefoxBinary instance.
(Via How can I tell selenium to start firefox with certain commandline options?.)
Or in Node perhaps by firefox.Options().setBinary(…).addArguments("-devtools")
Spotted the parameter in firefox -help | more, but alas, seems that not all information presented there as well as info given at Command_Line_Options MDC page are still valid. The -devtools one is missing at the MDN page at this moment.

Selenium IE driver not working with img element

I have this strange problem with img tags, my sample HTML code below
<html>
<input type='text' id='1111' MaxLength='10'/>
<img id='imageId001' title='hello' name='Done'/>
</html>
System.out.println(driver.findElement(By.id("imageId001")).getAttribute("title"));
Using Chrome Driver, getting output as hello.
However with IE 64/32 webdrivers,
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == imageId001(WARNING: The server did not provide any stacktrace information)
Whats wrong here???
Selenium WebDriver Version: 2.53.1.0
Internet Explorer : IE11
OS: Windows 7
JDK 1.7
After hours of effort spent, i figured out the cause. When i ran above code, i noticed a warning text displayed at bottom of IE as "Internet Explorer restricted this webpage from running scripts or ActiveX controls. " along with button "Allow blocked content". Since i have webdriverwait waiting for my img element, i clicked on this button and i saw that driver started printing the output which i was looking for (Same as chrome).
So i think driver object is not attached to browser if IE throws up some warnings like thsi. I solved it by disabling 2 options in Internet Options > Advanced Tab > Security section >
Allow active content from CDs to run on My Computer -> Set to Yes (ticked)
Allow active content to run in files on My Computer -> Set to Yes (ticked)
After this, my driver started identifying objects and works like chrome.
Note: Also i wanted to narrow down if registry setting played a role here as suggested by #Grasshopper. So I did same experiment in my colleague's machine (doesn't have registry settings for IE11) and this resolved for him as well. So registry setting does not play role here.

ChromeDriver - Disable developer mode extensions pop up on Selenium WebDriver automation

I'm having the following issue:
When I'm running my automation tests, I keep getting the following alert "Disable Developer Mode Extension" in Chrome.
Is there a way to remove/disable this?. It is a blocker for me as it is making me fail some tests.
Thanks in advance
Did you try disabling the developer extensions with command line param?
Try with the following Selenium WebDriver java code:
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
driver = new ChromeDriver(options);
I cannot disable extensions because I'm developing & testing one.
What I'm doing to dismiss this popup is the following:
I load chrome with my extension using Selenium.
I then immediately create a new window (via the SendKeys(Control-N) method). This predictably brings up the "Disable Developer Mode Extensions" popup after 3 seconds in the new window.
I can't tell programmatically when it pops up (doesn't show in screenshots) so instead I simply wait 4 seconds.
Then I close the tab via driver.Close(); (which also closes this new window). Chrome takes that as "cancel", dismissing the popup, leaving the original window and tab.
I find this necessary because the popup interferes with normal selenium browser interaction, like SendKeys, which I'm using to switch tabs and windows.
As of Chromedriver v2.33, the correct way to avoid this message is to pass load-extension to the excludeSwitches argument of the chromeOptions object. The following Java code should do the trick, although I haven't tested it, as I am running Python:
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("excludeSwitches", Collections.singletonList("load-extension"));
As others have pointed out, the culprit is probably the Chrome Automation Extension, which is loaded automatically by Chromedriver when it launches Chrome.
Chromedriver v2.33 introduced the new switch to prevent the extensions from being loaded:
Updates to excludeSwitches capability that now allows to exclude --load-extension switch.
I suspect that this solution does not require you to disable all extensions. You should still be able to manually load others.
This has been automatically fixed with a combination of ChromeDriver.exe V2.23 + Chrome 53.0.
To understand which chrome version will work with which driver, we can use the following well detailed doc: https://sites.google.com/a/chromium.org/chromedriver/downloads
Enjoy Automated Testing!!
I worked around this issue by using AutoIT.
First, you'll need to create the script.
closechromewarning.au3:
WinWaitActive("[CLASS:Chrome_WidgetWin_1]")
Send("{ESC}")
The script needs to be compiled to a .exe, then place the .exe in the path so it can be run.
Function that closes the warning, using c# syntax:
public void CloseChromeDialog()
{
System.Threading.Thread.Sleep(5000);
Process.Start(#".\closechromewarning.exe");
}
Sleep(4000) did work, but I upped it to Sleep(5000) just to be sure.
Calling CloseChromeDialog():
if(browser == chrome) //pseudo code
CloseChromeDialog();
resolved in chrome 54 and chromedriver 2.25
I too faced this problem. The solution is, if you are using maven then just add:
-Dchrome.switches=--disable-extensions
It will disable all the extensions and you will not face this problem.
I am using selenium Webdriver 2.53 and chrome version 56.0.2924.87 and the chrome driver.exe which I am using is 2.27. with this combination it is working with the
System.setProperty("webdriver.chrome.driver", "./utilities/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
DesiredCapabilities caps = new DesiredCapabilities().chrome();
caps.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(caps);
Try to add setProperty above ChromeDriver instance
System.setProperty("webdriver.chrome.driver","C:/[PATH]/chromedriver.exe");
driver = new ChromeDriver(capabilities);
pywinauto works
import pywinauto
window_title = "Disable Developer Mode Extensions"
app = pywinauto.Application().connect(name_re=window_title)
win_ext = app.window(name=window_title)
win_ext.close()
This is because one of your extensions is running in developer mode. Go through your extension list and disable extensions one-by-one until you find the culprit(s).

How do I run Firebug within Selenium WebDriver (Selenium 2)?

What's the best way to activate Firebug in Firefox when running Selenium 2?
Edit: Ok, I realize "best" is open to interpretation, but the profile-based solution really used to be a pain with selenium 1.0. So any alternative is considered better until proved worse ;)
You can create your profile in code and dynamically add required add-ons. Let's assume that you saved Firebug XPI into the C:\FF_Profile folder as firebug.xpi (go to Firebug download page, right-click on the "Add To Firefox" and save as C:\FF_Profile\firebug.xpi).
In code:
final String firebugPath = "C:\\FF_Profile\\firebug.xpi";
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File(firebugPath));
// Add more if needed
WebDriver driver = new FirefoxDriver(profile);
This is described in WebDriver FAQ
Do you mean having firebug installed in the browser instance that webdriver launches? If so, you can pass an extension when you instantiate the driver, but the eaisest way is to create a firefox profile with firebug installed and then use the following code before you instantiate the driver:
System.setProperty("webdriver.firefox.profile", "NAME_OF_FIREFOX_PROFILE_WITH_FIREBUG");
Just reference your profile by name. Example in Ruby:
#driver = Selenium::WebDriver.for :firefox, :profile => "default"
Then, load Firefox normally, and add your desired extensions. They will now show up in your Selenium test runs.
Apparently the way the firefox-profile options are consumed has changed in Selenium WebDriver.
The old commandline (Selenium RC):
java -jar selenium-2.28.0.jar -firefoxProfileTemplate ~/.mozilla/firefox/3knu5vz0.selenium
Updated for WebDriver: (note that it wants the profile name rather than the directory)
java -jar selenium-2.28.0.jar -Dwebdriver.firefox.profile=selenium
modify your firefox location to something like
C:\Users\user-name\AppData\Roaming\Mozilla\Firefox\Profiles\sgmqi7hy.default
launch your firefox from selenium / webdriver
make all your required settings
close and restart firefox browser from selenium / webdriver
that's it, it solves your problem !!
I found a profiles.ini in ~/.mozialla/firefox/. In there was a profile named default, which I specified a like the following and then firefox was opened in test just like I opened it regularly (with all plugins etc).
java -jar selenium.jar -Dwebdriver.firefox.profile=default
If none of the above option works. Then try this.
1) Open terminal and type below command (close all existing firefox
sessions first)
firefox -p
2) This will open an option to create a new Firefox profile.
3) Create a profile lets say "SELENIUM".
4) Once the firefox is open straight away install firebug or any
other plugins extension that you want. once done close the window.
5) Now load this new profile via selenium , use below java
statements.
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("SELENIUM");
WebDriver driver = new FirefoxDriver(ffprofile);
6) Done. Enjoy.
I have observed that the firebug is adding to browser and it is disabled by default and not enabled ,when i add firebug to firefox at runtime by using webdriver. So to make it enable we may need to add the below line to profile.
profile.setEnableNativeEvents(true);
Assuming that, Firebug is installed. Your objective is to run Firebug. Firebug can be run/execute by pressing F12 key. So Firebug can be run by following command of Selenium WebDriver with Java:
Actions action = new Actions(driver);
action.sendKeys(Keys.F12).build().perform();

Categories