Skip to content

Commit

Permalink
Window Management
Browse files Browse the repository at this point in the history
  • Loading branch information
angiejones committed Jul 10, 2019
1 parent da81cc4 commit 051e946
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions webdriver_java/src/main/java/pages/HomePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public DynamicLoadingPage clickDynamicLoading(){
return new DynamicLoadingPage(driver);
}

public MultipleWindowsPage clickMultipleWindows(){
clickLink("Multiple Windows");
return new MultipleWindowsPage(driver);
}

private void clickLink(String linkText){
driver.findElement(By.linkText(linkText)).click();
}
Expand Down
18 changes: 18 additions & 0 deletions webdriver_java/src/main/java/pages/MultipleWindowsPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class MultipleWindowsPage {

private WebDriver driver;
private By clickHereLink = By.linkText("Click Here");

public MultipleWindowsPage(WebDriver driver){
this.driver = driver;
}

public void clickHere(){
driver.findElement(clickHereLink).click();
}
}
50 changes: 50 additions & 0 deletions webdriver_java/src/main/java/utils/WindowManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package utils;

import org.openqa.selenium.WebDriver;

public class WindowManager {

private WebDriver driver;
private WebDriver.Navigation navigate;

public WindowManager(WebDriver driver){
this.driver = driver;
navigate = driver.navigate();
}

public void goBack(){
navigate.back();
}

public void goForward(){
navigate.forward();
}

public void refreshPage(){
navigate.refresh();
}

public void goTo(String url){
navigate.to(url);
}

public void switchToTab(String tabTitle){
var windows = driver.getWindowHandles();

System.out.println("Number of tabs: " + windows.size());

System.out.println("Window handles:");
windows.forEach(System.out::println);

for(String window : windows){
System.out.println("Switching to window: " + window);
driver.switchTo().window(window);

System.out.println("Current window title: " + driver.getTitle());

if(tabTitle.equals(driver.getTitle())){
break;
}
}
}
}
5 changes: 5 additions & 0 deletions webdriver_java/src/test/java/base/BaseTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import pages.HomePage;
import utils.WindowManager;

public class BaseTests {

Expand All @@ -29,4 +30,8 @@ public void goHome(){
public void tearDown(){
driver.quit();
}

public WindowManager getWindowManager(){
return new WindowManager(driver);
}
}
22 changes: 22 additions & 0 deletions webdriver_java/src/test/java/navigation/NavigationTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package navigation;

import base.BaseTests;
import org.testng.annotations.Test;

public class NavigationTests extends BaseTests {

@Test
public void testNavigator(){
homePage.clickDynamicLoading().clickExample1();
getWindowManager().goBack();
getWindowManager().refreshPage();
getWindowManager().goForward();
getWindowManager().goTo("https://google.com");
}

@Test
public void testSwitchTab(){
homePage.clickMultipleWindows().clickHere();
getWindowManager().switchToTab("New Window");
}
}

0 comments on commit 051e946

Please sign in to comment.