Created
October 8, 2022 07:13
-
-
Save krmahadevan/cf08ac0b47127bba5f79ea68ce2fd4ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.springpoc.test; | |
import com.springpoc.config.SpringConfig; | |
import com.springpoc.util.WebDriverUtils; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; | |
import org.testng.annotations.AfterMethod; | |
import org.testng.annotations.BeforeClass; | |
@SpringBootTest(classes = {SpringConfig.class}) | |
public class BaseTest extends AbstractTestNGSpringContextTests { | |
@Autowired | |
WebDriverUtils webDriverUtils; | |
@AfterMethod(alwaysRun = true) | |
public void cleanup() { | |
webDriverUtils.cleanup(); | |
} | |
@BeforeClass(alwaysRun = true) | |
@Override | |
protected final void springTestContextPrepareTestInstance() throws Exception { | |
super.springTestContextPrepareTestInstance(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.springpoc.util; | |
import static org.assertj.core.api.Assertions.assertThat; | |
import static org.awaitility.Awaitility.await; | |
import io.github.bonigarcia.wdm.WebDriverManager; | |
import java.time.Duration; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.openqa.selenium.remote.RemoteWebDriver; | |
import org.springframework.context.annotation.Scope; | |
import org.springframework.stereotype.Service; | |
@Service | |
@Scope("thread") | |
public class WebDriverUtils { | |
private static final ThreadLocal<RemoteWebDriver> remoteWebDriver = ThreadLocal.withInitial( | |
() -> { | |
WebDriverManager.chromedriver().setup(); | |
return new ChromeDriver(); | |
}); | |
public void navigateToPage(final String URL) { | |
driver().get(URL); | |
setBrowserToFullScreen(); | |
} | |
protected void setBrowserToFullScreen() { | |
driver().manage().window().fullscreen(); | |
} | |
public void waitUntilClickable(final By selector) { | |
RemoteWebDriver driver = driver(); //Get the driver instance in current thread itself | |
//because await seems to run assertions in a new thread | |
await().catchUncaughtExceptions().ignoreExceptions() | |
.timeout(Duration.ofSeconds(3)) | |
.untilAsserted(() -> { | |
assertThat(driver.findElements(selector).size()).isPositive(); | |
assertThat(driver.findElement(selector).isDisplayed()).isTrue(); | |
}); | |
} | |
public void click(final By selector) { | |
waitUntilClickable(selector); | |
driver().findElement(selector).click(); | |
} | |
public void fill(final By selector, final String text) { | |
waitUntilClickable(selector); | |
WebElement textbox = driver().findElement(selector); | |
textbox.clear(); | |
textbox.sendKeys(text); | |
} | |
public boolean isDisplayed(final By selector) { | |
try { | |
waitUntilClickable(selector); | |
} catch (Exception e) { | |
return false; | |
} | |
return true; | |
} | |
private RemoteWebDriver driver() { | |
return remoteWebDriver.get(); | |
} | |
public void cleanup() { | |
remoteWebDriver.get().quit(); | |
remoteWebDriver.remove(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment