I want to verify broken links on a website and I am using this code:
package com.automation.test;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
public static int invalidLink;
String currentLink;
String temp;
public static void main(String[] args) throws IOException {
// Launch The Browser
WebDriver driver = new FirefoxDriver();
// Enter Url
driver.get("any web site");
// Get all the links url
List<WebElement> ele = driver.findElements(By.tagName("a"));
System.out.println("size:" + ele.size());
boolean isValid = false;
for (int i = 0; i < ele.size(); i++) {
// System.out.println(ele.get(i).getAttribute("href"));
isValid = getResponseCode(ele.get(i).getAttribute("href"));
if (isValid) {
System.out.println("ValidLinks:"
+ ele.get(i).getAttribute("href"));
} else {
System.out.println("InvalidLinks:"
+ ele.get(i).getAttribute("href"));
}
}
}
public static boolean getResponseCode(String urlString) {
boolean isValid = false;
try {
URL u = new URL(urlString);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod("GET");
h.connect();
System.out.println(h.getResponseCode());
if (h.getResponseCode() != 404) {
isValid = true;
}
} catch (Exception e) {
}
return isValid;
}
}
Try the following piece of code another Method::
package com.automation.test;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
public static int invalidLink;
String currentLink;
String temp;
public static void main(String[] args) throws IOException {
// Launch The Browser
WebDriver driver = new FirefoxDriver();
// Enter Url
driver.get("any web site");
// Get all the links url
List<WebElement> ele = driver.findElements(By.tagName("a"));
System.out.println("size:" + ele.size());
boolean isValid = false;
for (int i = 0; i < ele.size(); i++) {
// System.out.println(ele.get(i).getAttribute("href"));
isValid = getResponseCode(ele.get(i).getAttribute("href"));
if (isValid) {
System.out.println("ValidLinks:"
+ ele.get(i).getAttribute("href"));
} else {
System.out.println("InvalidLinks:"
+ ele.get(i).getAttribute("href"));
}
}
}
public static boolean getResponseCode(String urlString) {
boolean isValid = false;
try {
URL u = new URL(urlString);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod("GET");
h.connect();
System.out.println(h.getResponseCode());
if (h.getResponseCode() != 404) {
isValid = true;
}
} catch (Exception e) {
}
return isValid;
}
}
I have modified getResponseCode to return boolean values based on whether the url is valid(true) or invalid(false).
Hope this helps you
|
Tuesday, 18 October 2016
How to find broken links using Selenium WebDriver with Java
Functional Implementation in Selenium Webdriver Exp
Functional Implementation:
In this implementation we will split up the page on the basis of functionalities and will have the methods like LoginToGmailAsValidUser and LoginToGmailAsInvalidUser.
See the below code snippet::
public class GmailLoginPage
{
private final WebDriver driver;
//Page Object constructor which passes the driver context forward
public LoginPage(WebDriver driver) {
this.driver = driver;
}
By usernameloc = By.id("Email");
By passwordloc = By.id("Passwd");
By loginButtonloc = By.id("signIn");
public HomePage LoginToGmailAsValidUser(String username, String password)
{
driver.findElement(usernameloc ).sendKeys(username);
driver.findElement(passwordloc ).sendKeys(password);
driver.findElement(loginButtonloc ).click();
return new InboxPage(driver)
}
public GmailLoginPage LoginToGmailAsInvalidUser(String username, String password) {
driver.findElement(usernameloc ).sendKeys(username);
driver.findElement(passwordloc ).sendKeys(password);
driver.findElement(loginButtonloc ).click();
return this;
}
}
The benefit of this approach is that the page structure is completely abstracted from the test layer. For example an extra checkbox “stay signed in” has been added on the login page and it also needs to be selected while logging a user in. So we simply need to add the extra code to handle this checkbox in the same method of the page class and it will not have any impact on the test layer, as the tests will still call the same method to login.
Structural Implementation:
In this approach the page is divided structurally depending upon the number of elements on the page
which we need to interact with. See the below code snippet:
public class GmailLoginPage
{
private final WebDriver driver;
//Page Object constructor which passes the driver context forward
public LoginPage(WebDriver driver)
{
this.driver = driver;
}
By usernameloc = By.id("Email");
By passwordloc = By.id("Passwd");
By loginButtonloc = By.id("signIn");
public HomePage typeUsername(String username)
{
driver.findElement(usernameloc).sendKeys(username);
return this;
}
public HomePage typePassword(String password)
{
driver.findElement(passwordloc ).sendKeys(password);
return this;
}
public HomePage clickOnSignin(String password)
{
driver.findElement(loginButtonloc ).click();
return new InboxPage(driver)
}
}
Challenges in Implementing Page Object Model:
Page object model is a very effective design pattern provided it is implemented correctly. I would like to cover some
complex scenarios.
Whenever any pageObject service (method) results in to a new page navigation, then that new page should be returned by the method. Let’s take the above example of Gmail login and as we know the method LoginToGmail() will lead us to the Inbox page so this should have a return type of InboxPage. See the below code:
public HomePage LoginToGmail(String username, String password)
{
driver.findElement(usernameloc).sendKeys(username);
driver.findElement(passwordloc ).sendKeys(password);
driver.findElement(loginButtonloc ).click();
return new InboxPage(driver)
}
GmailLoginPage loginPage = new GmailLoginPage (driver);
InboxPage = loginPage.LoginToGmail(“username”,”password”);
@FindBy annotations Using in Java
@FndBy Annotations Using
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPage {
@FindBy(id="loginInput") //method used to find WebElement, in that case Id
WebElement loginTextbox;
@FindBy(id="passwordInput")
WebElement passwordTextBox;
@FindBy(xpath="//form[@id='loginForm']/button(contains(., 'Login')") //method = xpath
WebElement loginButton;
public void login(String username, String password){
// login method prepared according to Page Object Pattern
loginTextbox.sendKeys(username);
passwordTextBox.sendKeys(password);
loginButton.click();
// because WebElements were declared with @FindBy, we can use them without
// driver.find() method
}
}
And Tests class:
class Tests{
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
LoginPage loginPage = new LoginPage();
PageFactory.initElements(driver, loginPage); //PageFactory is used to find elements with @FindBy specified
loginPage.login("user", "pass");
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LoginPage {
@FindBy(id="loginInput") //method used to find WebElement, in that case Id
WebElement loginTextbox;
@FindBy(id="passwordInput")
WebElement passwordTextBox;
@FindBy(xpath="//form[@id='loginForm']/button(contains(., 'Login')") //method = xpath
WebElement loginButton;
public void login(String username, String password){
// login method prepared according to Page Object Pattern
loginTextbox.sendKeys(username);
passwordTextBox.sendKeys(password);
loginButton.click();
// because WebElements were declared with @FindBy, we can use them without
// driver.find() method
}
}
And Tests class:
class Tests{
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
LoginPage loginPage = new LoginPage();
PageFactory.initElements(driver, loginPage); //PageFactory is used to find elements with @FindBy specified
loginPage.login("user", "pass");
}
}
Subscribe to:
Posts (Atom)