Automation QA Testing Course Content

Particular Section links Validation using Selenium WebDriver

package linksprograms;

import java.time.Duration;
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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.netty.handler.timeout.TimeoutException;

public class LinkedinSectionLinksTest {

static WebDriver driver = null;

public static void main(String[] args) throws InterruptedException {
// set the chromedriver.exe file path
System.setProperty("webdriver.chrome.driver",
"D:\\webdriverjars\\executables\\chrome971\\chromedriver_win32\\chromedriver.exe");
// interface refvar=new implementedclass();
driver = new ChromeDriver();

// maximize the window
driver.manage().window().maximize();

// add implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(10000));

// open the url in browser
driver.get("https://www.linkedin.com/");

// create Object for WebDriverWait class
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(30000));

wait.until(ExpectedConditions.titleContains("LinkedIn: Log In or Sign Up"));
try {
// identify the General section
WebElement genSection = driver
.findElement(By.xpath("//div[contains(@class,'w-full flex justify-end')]/div[1]/ul"));
// fetch all the links from General section
List<WebElement> secLinksList = genSection.findElements(By.tagName("a"));
for (int i = 1; i <= secLinksList.size(); i++) {
System.out.println("***************************************************************");
// 1. get the text of each link --use getText();
String linkName = driver
.findElement(
By.xpath("//div[contains(@class,'w-full flex justify-end')]/div[1]/ul/li[" + i + "]/a"))
.getText();
System.out.println("link text is:" + linkName);
// 2.fetch each link url --use
String url = driver
.findElement(
By.xpath("//div[contains(@class,'w-full flex justify-end')]/div[1]/ul/li[" + i + "]/a"))
.getAttribute("href");
System.out.println(linkName + " link url is:" + url);
// 3.click on each link
driver.findElement(
By.xpath("//div[contains(@class,'w-full flex justify-end')]/div[1]/ul/li[" + i + "]/a"))
.click();
wait.until(ExpectedConditions.urlContains(driver.getCurrentUrl()));
System.out.println(linkName + " page title is:" + driver.getTitle());
// navigate back to home page
driver.navigate().back();
Thread.sleep(2000);
}
} catch (TimeoutException te) {
te.printStackTrace();
} finally {
// close the browser
if (driver != null) {
driver.quit();
}
}
}

}


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.