Selenium IDE: Verify broken link 404 not found? - selenium

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

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.

Link Click using selenium webdriver is giving timeout error, link is working fine on browser otherwise

I'm trying to write a piece of code using Testng in selenium, the problem is when i try to click a link, the code becomes unresponsive and gives error- Timed out receiving message from renderer
Tried driver.get("https://infostore.saiglobal.com/") instead of driver.findElement(By.linkText("Infostore")).click(); still remains unresponsive - Doesnot get directed to the webpage - https://infostore.saiglobal.com/
#Test
public void testSai() throws Exception {
driver.get("https://www.saiglobal.com/"); //open homepage
driver.findElement(By.id("CybotCookiebotDialogBodyButtonAccept")).click(); //accept cookie
driver.findElement(By.cssSelector("#sai-header > div > div.main-nav > div.store-box > div")).click(); //click on Login to open list
sleep(2);
driver.findElement(By.linkText("Infostore")).click(); //click on infostore to be redirected to https://infostore.saiglobal.com/
System.out.println(driver.getCurrentUrl());
Yes I have checked this issue and there is an workaround for this issue if you wish to have this.Try following code.
Take the href attribute value from the link element.
Delete all cookies.
driver.navigate().to(URL);
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + File.separator + "\\Executables\\Chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, 5);
Actions actions = new Actions(driver);
driver.get("https://www.saiglobal.com/");
driver.findElement(By.id("CybotCookiebotDialogBodyButtonAccept")).click();
WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='desktop-login']")));
actions.moveToElement(element).build().perform();
WebElement element1=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='desktop-login']/ul/li/a[contains(text(),'Infostore')]")));
String str1=element1.getAttribute("href");
driver.manage().deleteAllCookies();
driver.navigate().to(str1);
System.out.println(driver.getCurrentUrl());
}
This seems to be an error with Selenium that the developers have been dealing with for over a year. They never really give a concrete answer as to what causes it or how to fix it.
The first port of call would be to make sure that your browser and Selenium are compatible. Try opening a different URL that you know works, and if the error persists then there is likely an error with the compatibility of your selenium and web browser. If it works, then there is something wrong with your website.
For reference, I opened your website using Python Selenium, and had no issues loading or interacting with elements. So the error is local to the software you are using.
The issue can also be caused by sleeps(no idea why), so try removing any sleeps and see if this stops the issue.

How to handle favicon.ico in headless selenium with xvfb

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

SafarDriver Selenium Page title is incorrect after loading new page

I have a Selenium 2 test that uses a login form and then asserts that the login has succeeded by verifying the page title has changed.
The test is written in Java/JUnit, WebDriver and Fluent Selenium
fwd.title().within(secs(5)).shouldContain(partialTitle);
The tests work on Firefox and Chrome.
On Safari the page title is still the old login page title.
The screencast we have generated shows that the URL after a successful login has changed, but the title hasn't.
I think the SafariDriver is not waiting for the page load event to complete before the assertion.
I have the same issue!!!
My test fail because title is used for checking test "Step".
With Firefox and Chrome no problem; with Safari it's a problem!!!
I found this "hack" that work, but it's necessary to manually check the title after each "click":
def static void waitForPage(driver, String title) {
//Hack to make login working on Safari&IE
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().startsWith(title);
}
});
}
I use GEB + SPOCK and with safari driver there is a "know issue" about implicit/explicit wait (ref. https://code.google.com/p/selenium/issues/detail?id=5860).
So, a nice workaround for make test runnable on Safari browser is to wait for page title:
static at = {
waitFor { title == "Homepage" }
}
This will force the driver to wait until title change (the timeout is configurable).

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