Skip to content

Commit

Permalink
JavascriptExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
angiejones committed Jul 9, 2019
1 parent 9f83c37 commit da81cc4
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
10 changes: 10 additions & 0 deletions webdriver_java/src/main/java/pages/HomePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ public WysiwygEditorPage clickWysiwygEditor(){
return new WysiwygEditorPage(driver);
}

public LargeAndDeepDomPage clickLargeAndDeepDom(){
clickLink("Large & Deep DOM");
return new LargeAndDeepDomPage(driver);
}

public InfiniteScrollPage clickInfiniteScroll(){
clickLink("Infinite Scroll");
return new InfiniteScrollPage(driver);
}

public DynamicLoadingPage clickDynamicLoading(){
clickLink("Dynamic Loading");
return new DynamicLoadingPage(driver);
Expand Down
32 changes: 32 additions & 0 deletions webdriver_java/src/main/java/pages/InfiniteScrollPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pages;

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

public class InfiniteScrollPage {

private WebDriver driver;
By textBlocks = By.className("jscroll-added");

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

/**
* Scrolls until paragraph with index specified is in view
* @param index 1-based
*/
public void scrollToParagraph(int index){
String script = "window.scrollTo(0, document.body.scrollHeight)";
var jsExecutor = (JavascriptExecutor)driver;

while(getNumberOfParagraphsPresent() < index){
jsExecutor.executeScript(script);
}
}

private int getNumberOfParagraphsPresent(){
return driver.findElements(textBlocks).size();
}
}
22 changes: 22 additions & 0 deletions webdriver_java/src/main/java/pages/LargeAndDeepDomPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pages;

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

public class LargeAndDeepDomPage {

private WebDriver driver;
private By table = By.id("large-table");

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

public void scrollToTable(){
WebElement tableElement = driver.findElement(table);
String script = "arguments[0].scrollIntoView();";
((JavascriptExecutor)driver).executeScript(script, tableElement);
}
}
4 changes: 0 additions & 4 deletions webdriver_java/src/test/java/base/BaseTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import org.testng.annotations.BeforeMethod;
import pages.HomePage;

import java.util.concurrent.TimeUnit;


public class BaseTests {

private WebDriver driver;
Expand All @@ -19,7 +16,6 @@ public class BaseTests {
public void setUp(){
System.setProperty("webdriver.chrome.driver", "resources/chromedriver");
driver = new ChromeDriver();
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
goHome();
}

Expand Down
21 changes: 21 additions & 0 deletions webdriver_java/src/test/java/javascript/JavaScriptTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package javascript;

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

public class JavaScriptTests extends BaseTests {

/************************************************************
NOTE: We did not add assertions to these tests in the video
************************************************************/

@Test
public void testScrollToTable(){
homePage.clickLargeAndDeepDom().scrollToTable();
}

@Test
public void testScrollToFifthParagraph(){
homePage.clickInfiniteScroll().scrollToParagraph(5);
}
}
2 changes: 1 addition & 1 deletion webdriver_java/src/test/java/wait/WaitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public void testWaitUntilHidden(){
loadingPage.clickStart();
assertEquals(loadingPage.getLoadedText(), "Hello World!", "Loaded text incorrect");
}
}
}

0 comments on commit da81cc4

Please sign in to comment.