How to handle favicon.ico in headless selenium with xvfb - selenium

I have the various scenarios of selenium test script which is properly running on selenium web driver with firefox browser. when i running them in headless mode some of scenarios are running but some of them are not running some time but most of time it fails and throw the error like
onhandlernotfound/favicon.ico
and
pagenotfound/favicon.ico[onhandlernotfound/favicon.ico]
the screenshot attached - first and second.
Give me the solution as soon as possible
my test case failed and get the error that element is not currently visible so may not be interacted with
element is not currently visible so may not be interacted with

You can try to get favicon internet-path and try to download it(externally, not via the selenium).
If favicon will be not accessible -- there will be no ability to download. If its is accessible -- its will be shown in any of browser (in case of enabled images) =)
Maybe this is not the best way, but this will work.
As you didnt write your language, I can give you code to download image for c#:
string webPath = ""; /*favicon internet path*/
if (webPath != string.Empty)
{
try
{
System.Net.WebRequest request = System.Net.WebRequest.Create(webPath);
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
Bitmap bitmapImg = new Bitmap(responseStream);
return bitmapImg;
}
catch (System.Net.WebException)
{
}
}

Related

Selenium GeckoDriver failing with blank page

I'm getting a blank page when trying to verify the driver url. I have this set up in debug logging mode. This is the FirefoxWebDriver.
UPDATE: If I take out all webdriver options then the tests work on a server but do not work in a Docker container.
FINAL UPDATE: Switched over to Chrome and found success, but with some important caveats.
[TestMethod]
public void NavigatesToContainerSite()
{
WebDriverWait webDriverWait = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
_driver.Navigate().GoToUrl("http://www.example.com:8083/");
string url = _driver.Url;
Assert.AreEqual("http://www.example.com:8083/", url);
}
[ClassInitialize]
public static void SetupOnce(TestContext context)
{
_options = new FirefoxOptions();
Proxy p = new Proxy();
p.SocksProxy = proxy url;
p.SocksVersion = 5;
p.Kind = ProxyKind.Manual;
_options.Proxy = p;
_options.PageLoadStrategy = PageLoadStrategy.None;
_options.LogLevel = FirefoxDriverLogLevel.Debug;
_options.AddArguments("--no-sandbox", "--disable-software-rasterizer","--headless" ,"--whitelisted-ips=\"\"",
"--disable-infobars","--disable-gpu","--disable-dev-shm-usage","--disable-extensions");
_driver = new FirefoxDriver(_options);
}
Here is the logging aspect of the request. If I just create a passing test that navigates to the url it seems to work but when I try to get something off the page (url, page elements) it throws exception.
Question: does the 200 OK indicate the driver.navigate succeeded or does it just mean the request for the webdriver session succeeded?
I just put the example.com url in this code example to hide my real url.
"1662816403483\twebdriver::server\tDEBUG\t-> POST /session/b29b42e9-cef5-4ef3-a281-8aafb21ea8cc/url {\"url\":\"http://www.example.com:8083/?username=owen.charles\"}",
"1662816403484\tMarionette\tDEBUG\t0 -> [0,2,\"WebDriver:Navigate\",{\"url\":\"http://www.example.com:8083/"}]",
"1662816403487\tMarionette\tDEBUG\t0 <- [1,2,null,{\"value\":null}]",
"1662816403487\twebdriver::server\tDEBUG\t<- 200 OK {\"value\":null}",
"1662816403541\twebdriver::server\tDEBUG\t-> POST /session/b29b42e9-cef5-4ef3-a281-8aafb21ea8cc/url {\"url\":\"http://www.example.com:8083/"}",
"1662816403542\tMarionette\tDEBUG\t0 -> [0,3,\"WebDriver:Navigate\",{\"url\":\"http://www.example.com:8083/"}]",
"1662816403543\tMarionette\tDEBUG\t0 <- [1,3,null,{\"value\":null}]",
"1662816403543\twebdriver::server\tDEBUG\t<- 200 OK {\"value\":null}",
"1662816403580\twebdriver::server\tDEBUG\t-> GET /session/b29b42e9-cef5-4ef3-a281-8aafb21ea8cc/url ",
"1662816403581\tMarionette\tDEBUG\t0 -> [0,4,\"WebDriver:GetCurrentURL\",{}]",
"1662816403581\tMarionette\tDEBUG\t0 <- [1,4,null,{\"value\":\"about:blank\"}]",
"1662816403581\twebdriver::server\tDEBUG\t<- 200 OK {\"value\":\"about:blank\"}",
"1662816403657\twebdriver::server\tDEBUG\t-> DELETE /session/b29b42e9-cef5-4ef3-a281-8aafb21ea8cc ",
"1662816403658\tMarionette\tDEBUG\t0 -> [0,5,\"Marionette:Quit\",{\"flags\":[\"eForceQuit\"]}]",
"1662816403658\tMarionette\tINFO\tStopped listening on port 49197",
This method doesn't produce an error:
[TestMethod]
public void NavigatesToContainerSite()
{
WebDriverWait webDriverWait = new WebDriverWait(_driver, TimeSpan.FromSeconds(30));
_driver.Navigate().GoToUrl("http://www.example.com:8083");
Assert.AreEqual("one", "one");
}
No one answered this but I did come up with a solution so I will just post it here. I had enough issues with the GeckoWebDriver that I switched to ChromeWebDriver. But the solution I arrived at may resolve the Gecko issue as well.
Steps I took:
Downloaded Chrome, major version 105
Incorporated ChromeWebDriver, major version 105 (these two have to match!)
Set two arguments when I instantiate the ChromeWebDriver in code
ChromeOptions options = new ChromeOptions();
options.AddArguments("--no-sandbox","--headless");
return new ChromeDriver(options);
Then it came to changes in the dockerfile for deploying this in a container. Because I have a Windows Docker host, I must deploy to a Windows container. I found out that Chrome requires about four or five fonts, and their various iterations, for its default installation to work. The Windows container only has one font in the installation. If you pull up Chrome settings, you'll probably see these fonts. One is for serif fonts, another sans-serif, etc. These fonts are:
arial.ttf
arialbd.ttf
arialbi.ttf
ariali.ttf
arialn.ttf
arialnb.ttf
arialni.ttf
ariblk.ttf
consola.ttf
consolab.ttf
consolaz.ttf
times.ttf
timesbi.ttf
timesi.ttf
I had to manually install these fonts into the windowsservercore image of Windows.
The resulting dockerfile looks like this:
FROM mcr.microsoft.com/dotnet/sdk:6.0-windowsservercore-ltsc2019
COPY ./test /test
WORKDIR /test
SHELL ["powershell"]
RUN "Add-WindowsFeature Web-WebSockets"
RUN Fonts/Add-Font.ps1 Fonts/Fonts
RUN ["msiexec","/i","chrome.msi","/qn"]
ENTRYPOINT ["dotnet", "test", "--logger:trx"]
In addition, I had to allocate 2g of shared memory in my Docker run statement.
docker run --shm-size="2g"
I found that the Selenium tests kick off about 5 instances of the browser and the web driver, sometimes taking up 600 MB of RAM. This will disappear if you use the driver.quit method after test is complete.
And voila, everything green.

Selenium getting stuck on data:, (MS Edge)

I want to use Selenium to navigate to a website. Selenium opens up the browser but does not navigate further to the specified website URL, but gets stuck on the "data:," url. After some time time I get the following exception:
"The HTTP request to the remote WebDriver server for URL http://localhost:58504/session timed out after 60 seconds"
Note: I did not specify the 58504 port anymore, so I guess it is the default port that Selenium use?
I am programming in C# and using the following Nuget Packages:
https://www.nuget.org/packages/Selenium.WebDriver/4.1.1
https://www.nuget.org/packages/WebDriverManager/2.13.0
Here is the code:
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using WebDriverManager;
using WebDriverManager.DriverConfigs.Impl;
using WebDriverManager.Helpers;
public void VisitWebsite()
{
IWebDriver driver = null;
try
{
new DriverManager().SetUpDriver(new EdgeConfig(), VersionResolveStrategy.MatchingBrowser);
EdgeOptions options = new EdgeOptions();
options.AddArgument("--no-sandbox);
options.AddArgument("--disable-infobars");
options.AddArgument("--disable-dev-shm-usage");
options.AddArgument("--disable-browser-side-navigation");
options.AddArgument("--disable-extensions");
options.AddArgument("--dns-prefetch-disable");
options.AddArgument("--disable-gpu");
options.AddArgument("--disable-software-rastersizer");
driver = new EdgeDriver(options);
}
catch (Exception ex)
{
throw ex;
}
driver.Navigate().GoToUrl("https://www.google.com");
}
Where does it go wrong? Thanks!
I found the answer. It seems the DeveloperToolsAvailability policy for the MSEdge browser had a value that "blocks" Selenium from working. You can read more about it here: https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/?tabs=c-sharp#developer-tools-availability-policy

ChromeDriver Access To Mic/Camera For Selenium Test

I’m trying to run an end to end test for a video chat platform, using the ChromeDriver. I need to use the actual webcam stream on my computer to run the test. I’ve tried both versions of the following code, but neither actually defaults the mic/camera settings to allow.
let options = new Options();
options.set("preferences.profile.content_settings.exceptions.media_stream_camera.'<localhost-address>,*'.setting", 1)
options.set("preferences.profile.content_settings.media_stream_mic.'<localhost-address>,*'.setting", 1);
let driver = await new Builder().setChromeOptions(options).forBrowser("chrome").build();
let options = new Options();
options.setUserPreferences({"preferences.profile.content_settings.exceptions.media_stream_camera.'<localhost-address>,*'.setting": 1,
"preferences.profile.content_settings.media_stream_mic.'<localhost-address>,*'.setting": 1});
let driver = await new Builder().setChromeOptions(options).forBrowser("chrome").build();
The pop-up still comes up and I can’t find a way to interact with it. Using a fake device/UI doesn’t resolve the issue, because it’s incompatible (to my knowledge) with Twilio, which the platform also uses.
Does anyone know how to default the ChromeDriver to allow camera/mic access during testing?
Update: The following code runs so that the pop up doesn't appear. But when the driver gets to the point that pop up would show up, the browser instance quits and the test ends. Is it impossible to stream from a webcam via Selenium?
let options = new Options();
options.setUserPreferences({"profile.default_content_setting_values.media_stream_mic": 1,
"profile.default_content_setting_values.media_stream_camera": 1});
let driver = await new Builder().setChromeOptions(options).forBrowser(browser).build();

Handling Browser Authentication using Selenium

Does anyone know about handling Browser Authentication using Selenium or any other tool during automation?
EDIT in 2015:
This answer is outdated. WebDriver nowadays supports authentication! See How to handle authentication popup with Selenium WebDriver using Java
Original answer:
This is not handled very well by Selenium.
You can try using http://username:password#example.com/yourpage
instead of just http://example.com/yourpage
However, as far as I know, Firefox will still pop up a browser dialog requesting a confirmation.
You can try Robot if you're using Java (or any similar tool like AutoIt).
You could use driver.manage().addCookie() if you're using WebDriver.
Or a custom FirefoxProfile that has already passed the authentication once.
I spent days on this - literally.
Trying to get past browser level authentication within my company network to hit an application.
The solution was to use the 'unsername:password#' component within the URL, BUT to add a forward slash at the end of the login URL.
So total login URL looks like this (note the '/' after yourpage):
http://username:password#example.com/yourpage/
Works with Watir, Capybara and Selenium Webdriver.
Everything I have read on the Web didn't help me. So before making a request, like this:
driver.get(url);
you have to run a new thread like this:
RunScript runScript = new RunScript();
runScript.start();
In this case you are free to input login and password on another thread of follwing class
public class RunScript extends Thread {
#Override
public void run() {
try {
File file = new File("D:\\jacob-1.18-x86.dll");
System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
AutoItX autoIt = new AutoItX();
Thread.sleep(2000);
autoIt.winActivate("yourWindowName", "");
autoIt.winWaitActive("yourWindowName");
if (autoIt.winExists("yourWindowName")) {
autoIt.send("username{TAB}", false);
autoIt.send("password{Enter}", false);
}
}
} catch (InterruptedException ex) {
//
}
}
}
All the hacks via auto-it, sikuli, etc. just wasting your time when you'll run it in your CI solution, using several browser types / OS / Version / Resolutions etc.
The way to do it correctly is to identify the authentication actual method and perform a login using Rest protocol for instance.
I used it to get the JSESIONID cookie and insert it to the selenium driver.
hint on that: go to a non-exiting url of the domian first, then set the cookie, then go to the required url - you are logged-in.
use: rest client authentication to get the JSESSION ID
and With this information:
browser().navigate(foo.getUrl()+"non-exiting-url");
//the information got from the rest client login:
Cookie cookie = new Cookie(name, value, domain, path, expiry, isSecure, isHttpOnly);
try {
driver.manage().addCookie(cookie);
} catch (Exception e) {
System.out.println(e.toString());
}
browser().navigate(foo.getUrl());
you can use auto IT script to handle this problem
WinWaitActive("[CLASS:Chrome_WidgetWin_1]", "", time)
Send("user")
Send("{TAB}")
Send("pass")
Send("{ENTER}")
with Chrome 70 and other versions :
http://username:password#example.com/yourpage
You can use Java Robot class with Selenium 2 /Selenium WebDriver using Firefox
WebDriver driver = new FirefoxDriver();
driver.get("http://localhost:9990");
WebElement myDynamicElement = driver.findElement(By.id("app"));
Alert alert = driver.switchTo().alert();
try {
Robot robot = new Robot();
alert.sendKeys("username");
robot.keyPress(KeyEvent.VK_TAB);//go to password feild
robot.keyPress(KeyEvent.VK_P);
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException e) {
e.printStackTrace();
}
}
Using Selenium with Robot
http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Robot.html

Selenium IDE: Verify broken link 404 not found?

Any one have any idea whether Selenium IDE able to verify any broken link in page??
Short answer: yes, but probably not the best idea.
Selenium might not be the best program to do this - especially if you use only Selenium IDE.
Multiple programs exist with the sole purpose of testing broken links (do a little search), hell, even the W3C has a webapp for that.
That said, you could take the output from Selenium IDE formatted to your favourite language, program a Selenium test case with more advanced Unit Testing classes, include a while loop which would check for the existence of more links, and visit them / check headers, if you really want to do this through Selenium.
In short, you can't (yet). See a more full writeup of the situation at: How do I ask Selenium IDE to check a HTTP Status Code (e.g. 2XX, 404, 500)
Basically the selenium maintainers decided this function did not belong in selenium because error codes are read by machines not humans.
This sounds like a job for a simple spider script, for example using the wget on Linux
--spider
When invoked with this option, Wget will behave as a Web spider,
which means that it will not download the pages, just check that
they are there. For example, you can use Wget to check your book‐
marks:
wget --spider --force-html -i bookmarks.html
This feature needs much more work for Wget to get close to the
functionality of real web spiders.
You can use 404 status code to verify using REST web services.
Use gson and apache jars to get it.
public class Testing{
public static void main(String[] args) {
try{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to("https://www.amazon.in/");
List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Number of links : " + links.size());
for(WebElement e : links) {
String linkText = e.getAttribute("href");
System.out.println("Link -->>" +linkText);
if(linkText!=null && !linkText.isEmpty()) {
HttpPost post = new HttpPost(linkText);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse res = client.execute(post);
String s = res.getStatusLine().toString();
if(s.contains("404")) {
System.out.println("Navigated");
//your code to handle logic
} else {
//your code to handle logic with other response code
}
}
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
}
}