Skip to content

Instantly share code, notes, and snippets.

@angiejones
Created July 14, 2019 21:07
Embed
What would you like to do?
Answer Key for Chapter 3 of Selenium WebDriver Java Course on Test Automation University
package base;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class BaseTests {
private WebDriver driver;
public void setUp(){
System.setProperty("webdriver.chrome.driver", "resources/chromedriver");
driver = new ChromeDriver();
driver.get("https://the-internet.herokuapp.com/");
driver.findElement(By.linkText("Shifting Content")).click();
driver.findElement(By.linkText(("Example 1: Menu Element"))).click();
List<WebElement> menuItems = driver.findElements(By.tagName("li"));
System.out.println("Number of menu items: " + menuItems.size());
driver.quit();
}
public static void main(String args[]){
BaseTests test = new BaseTests();
test.setUp();
}
}
@venkatams
Copy link

Hi Angie,
I've used the following command in line #20:
driver.findElement(By.partialLinkText("Example 1")).click();

This has worked just as well for me.
Thanks

@lakshmiviln
Copy link

just went one step ahead and printed all the menu items as well, with following code

List menuElements = driver.findElements(By.tagName("li"));
for( WebElement menu : menuElements){
System.out.println(menu.getText());

@mohammedyouness
Copy link

mohammedyouness commented Apr 23, 2020

I think using CSS selectors is cleaner than using LinkText as it's more error-prone If I misspelled the link text or just forgot any spaces between the words
driver.findElement(By.cssSelector("a[href='/shifting_content']")).click();
driver.findElement(By.cssSelector("a[href='/shifting_content/menu']")).click();
List menuItems = driver.findElements(By.tagName("li"));
System.out.println(menuItems.size());

@sanjia022
Copy link

Hi Angie,

I used CSS and Xpath selector. Here is the code:

            WebElement shifting = driver.findElement(By.xpath("//div[@id='content']/ul//a[@href='/shifting_content']"));
	shifting.click();
	WebElement menuItems = driver.findElement(By.cssSelector("div#content > div > a:nth-of-type(1)"));
	menuItems.click();
	List<WebElement> lists = driver.findElements(By.tagName("li"));
	System.out.println(lists.size());

@helenayele
Copy link

Thanks Angie,
Thanks for the group members in this course.
My code looks like this:
WebElement shifitingContentLink = driver.findElement(By.linkText("Shifting Content"));
shifitingContentLink.click();
WebElement exampleOne = driver.findElement(By.partialLinkText("Menu Element"));
exampleOne.click();
List lists = driver.findElements(By.tagName("li"));
System.out.println(lists.size());

And working fine

@Dksoni81291
Copy link

I've used below code- combination of CSS selector and Xpath.

    driver.get("https://the-internet.herokuapp.com/");
    driver.manage().window().fullscreen();

    WebElement Inputlink = driver.findElement(By.linkText("Shifting Content"));
    Inputlink.click();

    WebElement Option = driver.findElement(By.cssSelector("a[href='/shifting_content/menu']"));
    Option.click();

    List<WebElement> Weblinks =  driver.findElements(By.xpath("//ul/li/a"));
    System.out.println(Weblinks.size());
    
    for(WebElement menu: Weblinks){
        System.out.println(menu.getText());
    }

@dutchviewjyothi
Copy link

Hey Angie, My code for the above scenario looks like this:

System.setProperty("webdriver.chrome.driver","C:\chrome\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://the-internet.herokuapp.com/");
driver.manage().window().maximize(); // to maximize the window
System.out.println(driver.getTitle());
WebElement Shift = driver.findElement(By.linkText("Shifting Content"));
Shift.click();
WebElement Menu = driver.findElement(By.linkText("Example 1: Menu Element"));
Menu.click();
List lists = driver.findElements(By.tagName("a"));
System.out.println(lists.size());

Thank you

@ayagharieb
Copy link

ayagharieb commented Aug 31, 2021

Hello angi,
thanks alot for your easy and great course,
i'm very enthusiastic while studying it ,
Here you are "my code" but i add from your solution just this sentence ["Number of menu items: " + ] to my code :

package base;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class BaseTests {
private WebDriver driver ;
public void setup()
{
System.setProperty("webdriver.chrome.driver","resources\chromedriver.exe");
driver= new ChromeDriver();
driver.get("https://the-internet.herokuapp.com/");

    WebElement inputslink = driver.findElement(By.xpath("//*[text()='Shifting Content']"));
    inputslink.click();
    WebElement MenuElement = driver.findElement(By.xpath("//a[text()='Example 1: Menu Element']"));
    MenuElement.click();
    List<WebElement> CountMenuElement = driver.findElements(By.xpath("//div[@class='example']/ul/li"));
    System.out.println("Number of menu items: " +CountMenuElement.size());
    System.out.println(driver.getTitle());
}
public static void main (String args [] )
{
    BaseTests test = new BaseTests();
    test.setup();
}

}

@samiullah
Copy link

`package TESTIM;

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.testng.Assert;
import org.testng.annotations.*;

public class ShiftingContent {

WebDriver driver;


@BeforeClass
public void setup() {
	
	System.setProperty("webdriver.chrome.driver","/Users/sami/eclipse-workspace/Testa/chromedriver 3");
		 
	driver = new ChromeDriver();
	driver.manage().window().maximize();
			
}


@Test
public void OpenBrowser() {
	driver.get("https://the-internet.herokuapp.com/");
}


@Test
public void Sigin() throws InterruptedException {
	WebElement input = driver.findElement(By.linkText("Shifting Content"));
	input.click();
	Thread.sleep(3000);
	
	WebElement example = driver.findElement(By.linkText("Example 1: Menu Element"));
	example.click();
	
	Thread.sleep(3000);
	
	List<WebElement> menus = driver.findElements(By.tagName("li"));
	System.out.println(menus.size());
	

}

@afterclass
public void closeBrowser() {
driver.quit();
System.out.println("Quit browser");

}

}
`

@qaolgawu
Copy link

Hello Angie,

please find my code below for Ch3. Selenium WebDriver Java Course on Test Automation University. Thank you!

package base;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

import static java.lang.System.out;

public class BaseTests {

private WebDriver driver;

public void setUp() {
    System.setProperty("webdriver.chrome.driver", "resources/chromedriver");
    driver = new ChromeDriver();
    driver.get("https://the-internet.herokuapp.com/");

    WebElement inputsLink = driver.findElement(By.linkText("Shifting Content"));
    inputsLink.click();

    WebElement inputsLink2 = driver.findElement(By.linkText("Example 1: Menu Element"));
    inputsLink2.click();

    List<WebElement> menuItems = driver.findElements(By.tagName("li"));
    System.out.println("Number of menu items: " + menuItems.size());

    driver.quit();
}

public static void main(String args[]) {
    BaseTests test = new BaseTests();
    test.setUp();
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment