Can anyone please help me with Selenium + Java + IE + Windows code and settings to be done step by step? There are so many conflicting answers over the web and none of those is working (due to lack of my understanding perhaps, I am basically shell and python programmer and shifted to test automation recently). Please help me with code and required settings. Screenshots of settings will be highly appreciated (I am new to windows as a programming environment, I have extensive command line programming experience on *nix systems on shell and python as mentioned earlier).
The problem is that the "Internet Explorer" window is opened, however the test cases are not executed at all and after waiting for some time the Eclipse console shows error.
Thanks,
P Ashwin
Ensure you download the IE Driver Server. You'll need 32 bit or 64 bit to match your OSes "bitness."
The executable should be on your path. You can point to it in code as Aramin called out above; however, I prefer to have the executable on the path--doing it in code is just one more configuration mess you have to deal with if you run on different systems.
Note that you'll need to restart any shells/command windows after you add the server's executable to your system environment variables.
After that it's just a matter of firing it up in code and looks like any other WebDriver:
WebDriver browser;
public void initialize() {
browser = new InternetExplorerDriver();
browser.navigate().to("http://the-internet.herokuapp.com");
}
Note: This is from a canned set of demos I use for training. It runs under Cucumber, normally with an #Before tag. I took that out to avoid confusion.
HTH
Code :
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
WebDriver driver= new InternetExplorerDriver();
driver.get("http://www.w3schools.com");
Along with this you need to enable scripting for IE(Internet Options->Security->Custom level ) as shown in ]1
Hope this will help you..
Related
I am trying to run the org.openqa.selenium.firefox.FirefoxDriver in Java.
I would like to open a browser window without the address bar and other functionalities like page-forward and so on.
Actually I would like to have a plain window with the displayed web content and even disabled context menu.
How to archive that.
I tried with the code:
FirefoxOptions op = new FirefoxOptions()
.addPreference("browser.startup.page", 1)
.addPreference("browser.startup.homepage", "https://google.com");
WebDriver driver = new FirefoxDriver();
but could not find any good, easy understandable documentation for preferences or arguments (addArgument(...)).
Could anyone give me a hint here?
These resources also did not give me a good understandable help:
https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions
https://searchfox.org/mozilla-central/source/modules/libpref/init/all.js
How to correctly use FirefoxOptions() and its arguments to pass it to the FirefoxDriver() constructor
Other threads I found on the internet are many years old.
As bonus, the same links for Chrome would also be appreciated.
This is a solution what I almost perfectly want. It does not deal with Selenium/Selenide, which is fully ok with me. I made it work nicely with Chrome browser.
See: https://github.com/greyshine/javaspring-embedded-browser
Most interesting files are the index.html and BrowserAdaptor.java.
What I still miss:
A nice way independent on the OS on how to locate the executable for the browser; see application.properties
Firefox does not open with a pure screen but still has tabs and some browser functions. What would be the startup parameters (see application.properties) for firefox to correctly start.
Same story for the Edge browser. Startup parameters, etc...
Bringing Selenide into the question was based, that I seen that this must be somehow possible.
INTRO:
I am a student using Java Selenium for GUI testing. I cannot get test cases to run on my desktop but other members of my team are able to.
WHAT TECH/SOFTWARE IS BEING USED
We are using JDK 14, IntelliJ Community Edition, BitBucket for our repo, and JUnit 4.
WHAT AM I TESTING
I am testing a sample single page application. I add a task, use a date picker to add a date, and then I click the "Add Item" button. However the click to the button isn't registered and the task isn't added to the list. If I restart my computer the first #Test works but the second one fails to clear the cookies and the second #Test fails.
DETAILED VERSION:
I am coming from a Surface Book 2 to a Alienware R10 desktop. My team was able to pull the project and run all of the test cases with no problem. However, I was unable to. The same program works on my SB2 but not on the Alienware R10 (AMD 3900 Processor).
WHAT I HAVE TRIED:
I used both a locally stored Chrome Driver and I used Boni Garcia's
WebDriverManager.
The project is built using Maven so I have tried to use both
dependencies and also add the selenium files directly to my project
structure with no success.
I reformatted my new R10 and tried to install everything again. I
carefully added the JAVA_HOME and MAVEN_HOME to my system path.
I tried to use ChromeOptions after seeing some posts on SOF and none
of those worked either.
The QA dev who leads us tried to help me for about 40 minutes and
couldn't figure it out. He said the problem isn't with my IDE but it
is with the Chrome cookies. I will have to clear the cookies every
time I want to run a JUnit Selenium test. However, other members of
my team do not have to do this and this doesn't always work.
EXTRA DETAILS:
I got a new Alienware R10 with the AMD Ryzen 3900 chip. I have added
JAVA_HOME and MAVEN_HOME to my Path and environmental variables.
I may be adding to the project structure or dependencies incorrectly.
This is my first post so please let me know if I should format the
question better or if I left any important details out.
CONCLUSION
I am pretty worried I wanted to study and practice over the weekend but I have put a lot of time into fixing this. My next idea is to use Eclipse instead of IntelliJ and see if I can get it running there. Any advice here would be helpful.
EDIT
Eclipse is not working either and has the same problem. This tells me the problem is definitely not related to the IDE. It is likely a CPU or Browser related issue. I also tried using other drivers through the WebDriverManger dependency.
Upon further research it seems to register clicks and actually add to the Tasks if I add a few Thread.sleep() functions into my code instead of the ExpectedConditions and element visability. The problem may be stemming from using the ExpectedConditions API but again this is working for my teammates and not for me. Could this potentially be a AMD chipset issue?
The element visibility may be the wrong function to use here but it did work on my teammates machines so that is why I didn't try to look for an alternative there. The function we are using looks at the DOM tree and we may instead want to see the visibility on the browser instead. I had difficulty testing this out as I didn't have a laptop to work with.
I reformatted my old laptop so it could be sold since I had this new PC set up but I will use it to try another function to see if it brings a different result.
EDIT2:
Observation/Guess
I am going to guess the problem is with the AMD 3900 CPU. The Thread.sleep() changes the behaviour to at least click my 'Add Item' button. It still does fail the tests I put it through and the ExpectedConditions API still doesn't work.
I am going to be returning the Alienware R10 and going to get an intel CPU R10 or build my own PC.
This is just an educated guess based on the difference in the behavior seen between my and myteammates' machines they have intel I have AMD.
If you are facing a similar issue please confirm whether you are using AMD or Intel in this thread. It will help others as well. I'm sure there are work arounds but they did not work for me. I also tried using ChromeOptions to disable the GPU and a few other options.
However there was no success and unless there is a better answer I'm blaming the AMD 3900 CPU.
Thanks and please contribute to this thread if you are facing a similar issue or have found a better answer. Any speculation could be helpful too.
I agree with the QA on your team.
This is not IDE related. I would suspect that manipulating the DesiredCapabilities of the WebDriver would resolve this issue. Given that you are using the bonigarcia WebDriverManager dependency, I would recommend atleat attempting the code below to see if the WebDriver can be created properly.
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
DesiredCapabilities chromeDesiredCapabilities = DesiredCapabilities.chrome();
WebDriver webdriver = new ChromeDriver(chromeDesiredCapabilities)
}
I am trying to use Headless feature of the Chrome to convert a html to pdf. However, i am not getting output at all. Console doesn't show any error as well. I am running below commands in my windows m/c.
chrome --headless --disable-gpu --print-to-pdf
I tried all the various options. Nothing is being generated. I am having chrome version 60
Command Line --print-to-pdf
By default, --print-to-pdf attempts to create a PDF in the User Directory. By default, that user directory is where the actual chrome binary is stored, which is the specific version folder for the version you're running - for example, "C:\Program Files (x86)\Google\Chrome\Application\61.0.3163.100". And, by default... Chrome is not allowed to write to this folder. You can watch it try, and fail, by adding --enable-logging to your command.
So unfortunately, by default, this command fails.*
You can solve this by either providing a path in the argument, where Chrome can write - like
--print-to-pdf="C:\Users\Jane\test.pdf"
Or, you can change the User Directory:
--user-data-dir="C:\Users\Jane"
One reason you might prefer to change the User Directory is if you want the PDF to automatically receive its name from the webpage; Chrome looks at the title tag and then dumps it like <title>My Page</title> => My-Page.pdf
*I think this default behavior is super confusing, and should be filed as a bug against Chrome. However, apparently part of the Chrome team is outright opposed to the mere existence of this command line option, and instead believe it would be better to force everyone using it to get a node.js build going with Puppeteer and the flag removed outright.
Limitations of Command Line on Windows
Invoking chrome in this way will work fine for example in a local dev env on IIS Express with Visual Studio, but it will fail, even in headless mode, on a server running IIS, because IIS users are not given interactive/desktop permissions, and the way chrome grabs this PDF actually requires interactive/desktop permissions. There are complicated ways to provide those permissions, but anyplace you read up on how begins with DON'T PROVIDE INTERACTIVE/DESKTOP PERMISSIONS. Further, the above risk of Chrome one day getting rid of the command-line makes working even harder to get it working an iffy proposition.
Alternatives to chrome command line
wkhtmltopdf
Behind the scenes Chrome simply uses wkhtmltopdf. I haven't tried it but it's likely this will get the job done. The one minor risk is that when producing PDFs in Chrome, testing is obvious: View the page in Chrome. Open Print Preview if you're nervous. In wkhtmltopdf, it's actually a different build of Chromium, and that may produce rendering differences. Maybe.
Selenium
Another alternative is to get ahead of the group looking to get rid of --print-to-pdf and use the browser dev API (via Selenium) as they prefer.**
private static void pdfSeleniumImpl(string url, string pdfPath)
{
var options = new OpenQA.Selenium.Chrome.ChromeOptions();
options.AddArgument("headless");
using (var chrome = new OpenQA.Selenium.Chrome.ChromeDriver(options))
{
chrome.Url = url;
var printToPdfOpts = new Dictionary<string, object>();
var resultDict = (Dictionary<string, object>)
chrome.ExecuteChromeCommandWithResult(
"Page.printToPDF", printToPdfOpts);
dynamic result = new DDict(resultDict);
string data = result.data;
var pdfFile = Convert.FromBase64String(data);
System.IO.File.WriteAllBytes(pdfPath, pdfFile);
}
}
The DDict above is the GracefulDynamicDictionary from another of my answers.
https://www.nuget.org/packages/GracefulDynamicDictionary/
https://github.com/b9chris/GracefulDynamicDictionary
https://stackoverflow.com/a/24192518/176877
Ideally this would be async, since all the calls to Selenium are actually network commands, and writing that file could take a lot of Disk IO. The data returned from Chrome is actually a Stream as well. However Selenium's conventionally used library does not use async at all unfortunately, so it would take upgrading that library or identifying a solid async Selenium library for .Net to really do this right.
https://github.com/puppeteer/puppeteer/blob/master/lib/Page.js#L1007
https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
**The Page.pdf chrome Dev API command is also deprecated, so if that contingent gets their way, neither the command line nor the Dev API will work. That said it looks like those lobbying to wreck it gave up 2 years ago.
This is working:
chrome --headless --disable-gpu --print-to-pdf=file1.pdf https://www.google.co.in/
creates file in the folder: C:\Program Files (x86)\Google\Chrome\Application\61.0.3163.100.
Do not forget to open your terminal/cmd with admin rights :) Otherwise it will just not save the file at all.
I was missing "=" after print-to-pdf command.
The correct command is:
chrome --headless --disable-gpu --print-to-pdf="C:/temp/name.pdf" https://www.google.com/
Now it is working.
extending the brilliantly simple answer by suraj, I created a small function that is in my sourced path so it works like a CLI tool:
function webtopdf(){
chromium-browser --headless --disable-gpu --print-to-pdf=$2 $1
}
so a quick
webtopdf https://goo.com/some-article some-article.pdf
does the job for me now
This worked for me in windows
start chrome --headless --disable-gpu
--print-to-pdf=C:\Users\username\pdfs\chrome.pdf --no-margins https://www.google.com
Currently, this is only available for Linux and Mac OS.
I am newbie in automation world. Recently, I created some automation script for my work. Its working good. The problems that I am facing are:
This script work in normal speed in my machine but if i use the same script in my friend's computer, its super super fast. So fast that its braking the automation steps.
This automation works good on my machine with Firefox driver but if i try to use Chrome driver, again its super super fast that it breaks the automation step.
Is there any solution for this? Why automation acts differently in different machine? Is there a way to configure the speed.,..?
Yes kiran you can add explict wait. Please find the code below :
WebDriverWait wait= new WebDriverWait(driver,20 );// Explicit wait()
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("xyz")));
you must be using different driver exes with different machine.. I have observed pathetic performance while using IEdriver for 64 bit. Try using same webdriver exes on both the machines. Thanks!
Dear if it work fast its good but if its getting failed because of not fining the particular field you are locating, in that case you can use the Implicit or Explicit wait so that the your driver should wait until it doesn't found particular item you are locating.
You can also use Thread.sleep(5000) but that is not a good practice.
I've got a little test suite working on my laptop which I've tried to transfer over to my work desktop. To, hopefully, make things easier, I used the same version of Java and maven. However, typically, it's not been so easy :)
When I started Firefox, I got a "Firefox has stopped working" message box. The message box indicated the issue lay with "xul.dll" - so I closed the box and Firefox opened. It does this everytime I open Firefox.
I have even stripped out all my allure and other code and just ran a simple case:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.uk");
However this doesn't work either.
It was then I checked the versions of Firefox were the same, turns out they weren't - but the Firefox updated fixed that. But my test still passes on the laptop, but fails on the desktop. I can't see anything I have missed, I can see one possible issue - the work desktop has Kaspersky installed.
Has anyone ever experienced what I've described?
I have also observed that antivirus causes the Firefox to hang during the launch. Try disabling the antivirus and rerun your script to verify that in your case also the antivirus/firewall is causing the issue.
Check the version of Firefox on both computers. I have two Windows machines with Selenium 2.45.0. One machine has Firefox 37.0.2 and the code below executes fine. The other machine I just updated to Firefox 38.0.1 and I receive that error message box.
#Test
public void testFirefoxDriver() throws Throwable
{
new FirefoxDriver();
}
I've been using Selenium for over a year now. Our production server automatically updates Firefox and this occasionally causes incompatibilities between Firefox and Selenium. I would expect to see a new Selenium release very soon.
Firefox 38.x is not supported by Selenium 2.45.1. Either you have to roll back your firefox to 37.x or wait until next revision of Selenium.
https://github.com/SeleniumHQ/selenium/issues/437
I've got this same issue. Funnily enough it was working yesterday morning I started getting it yesterday afternoon and now it appears to be messing up my tests.
I have Selenium 2.45.0 & Firefox 38.0.1.
My tests were running perfectly before this started happening and nothing has changed in my code. I've also ran another test suite just to make sure that it wasn't the one I was working on. The same thing happened.
I've just uninstalled firefox downloaded the previous version and they now work again with no crash.