Navigation Command in Selenium Webdriver

0

Navigation Command in Selenium Webdriver,Selenium Webdriver Tutorial:-
It is used to navigate from one web page to another without any action(hyperlink, button click)
Selenium Web Driver provide multiple navigation command to perform the operation.
1) navigate().to() :-  it is used to navigate particular url of web page according to condition
WebDriver driver= new ChromeDriver();
driver.navigate().to("http://shivaconceptsolution.com/test.html");
2)  driver.navigate().back(); :-  it is used to press back button of web browser
3)  driver.navigate().forward():-  It is used to press forward button of web browser
4)  driver.navigate().refresh():-  It is used to refresh current web content of web pages.
Example create automation script to find out total working hyperlink and broken link.
Solution:-
package scs;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class CheckHyperlink {
public static void main(String[] args) {
int a=0,b=0;
System.setProperty("webdriver.chrome.driver","c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://eroomrent.in/index.php");
List<WebElement> ele = driver.findElements(By.tagName("a"));
Iterator it = ele.iterator();
ArrayList arrlist = new ArrayList();
while(it.hasNext())
{
WebElement e =(WebElement)it.next();
String u =e.getAttribute("href");
if(u.equals("") || u.equals("#"))
{
a++;
}
else
{
b++;
}
arrlist.add(u);
System.out.println(u);
}
for(Object o: arrlist)
{
driver.navigate().to(o.toString());
driver.navigate().back();
driver.navigate().refresh();
}
System.out.println("Total Hyperlinks are "+ele.size());
System.out.println("Total non Working Hyperlinks are "+a);
System.out.println("Total working Hyperlinks are "+b);
}
}
This example using for loop and for each loop also:-
package scs;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.*;
public class Allhyperlink {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
int a=0,b=0;
driver.get("https://www.eroomrent.in");
List<WebElement> s = driver.findElements(By.tagName("a"));
//Iterator it = s.iterator();
ArrayList arrlist = new ArrayList();
/*for(int i=0;i<s.size();i++)
{
WebElement e =(WebElement)s.get(i);
String u =e.getAttribute("href");
if(u.equals("") || u.equals("#"))
{
a++;
}
else
{
b++;
}
arrlist.add(u);
System.out.println(u);
}*/
for(WebElement o:s)
{
WebElement e =(WebElement)o;
String u =e.getAttribute("href");
if(u.equals("") || u.equals("#"))
{
a++;
}
else
{
b++;
}
arrlist.add(u);
System.out.println(u);
}
/* for(Object o: arrlist)
{
driver.navigate().to(o.toString());
driver.navigate().back();
driver.navigate().refresh();
}*/
System.out.println("Total Hyperlinks are "+s.size());
System.out.println("Total non Working Hyperlinks are "+a);
System.out.println("Total working Hyperlinks are "+b);
driver.quit();
       }
}
Create Automation Script to count total header links and footer links in Eroomrent.in?
package scs;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class HeaderHyperlink {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","c://chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://eroomrent.in/");
WebElement header = driver.findElement(By.tagName("nav"));
int c = header.findElements(By.tagName("a")).size();
System.out.println(c);
WebElement f = driver.findElement(By.tagName("footer"));
int c1 = f.findElements(By.tagName("a")).size();
System.out.println(c1);
}
}

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)