Skip to content

Instantly share code, notes, and snippets.

View eliasnogueira's full-sized avatar
🇧🇷

Elias Nogueira eliasnogueira

🇧🇷
View GitHub Profile
@eliasnogueira
eliasnogueira / PageObjectExample.java
Created August 21, 2022 11:38
Page Object example waiting for an element in the action method
public class PageObjectExample {
private WebDriver driver;
@FindBy(id = "email")
private WebElement email;
@FindBy(id = "password")
private WebElement password;
@eliasnogueira
eliasnogueira / PageObjectsWithPageFactory.java
Created August 21, 2022 11:24
Page Object example using the PageFactory class
public class PageObjectExample {
@FindBy(id = "email")
private WebElement email;
@FindBy(id = "password")
private WebElement password;
@FindBy(name = "next")
private WebElement next;
@eliasnogueira
eliasnogueira / PageObjectSimpleExample.java
Created August 21, 2022 11:19
Simple Page Objects example without the usage of Page Factory
public class PageObjectExample {
private final WebDriver driver;
public PageObjectExample(WebDriver driver) {
this.driver = driver;
}
public void login(String email, String password) {
driver.findElement(By.id("email")).sendKeys(email);
@eliasnogueira
eliasnogueira / PageObjectExample.java
Last active August 21, 2022 11:11
Simple Page Objects example waiting for an element in the constructor
public class PageObjectExample {
private WebDriver driver;
@FindBy(id = "email")
private WebElement email;
@FindBy(id = "password")
private WebElement password;
class BookRoomWebTest extends BaseWeb {
@Test
void bookARoom() {
driver.navigate().to(configuration.url() +
"how-to-create-lean-test-automation-architecture-for-web-using-java-libraries");
assertThat(driver.findElement(By.cssSelector(".hestia-title.title-in-content")).getText()).
isEqualTo("How to create Lean Test Automation Architecture for Web using Java libraries");
}
@eliasnogueira
eliasnogueira / BaseWeb.java
Created July 8, 2022 05:06
BaseTest class for the selenium-java-browser-factory project
public class BaseWeb {
protected WebDriver driver;
protected Configuration configuration;
@BeforeEach
public void preCondition() {
configuration = configuration();
driver = new DriverFactory().createInstance(configuration().browser());
@eliasnogueira
eliasnogueira / DriverFactory.java
Created July 7, 2022 16:46
Code snippet of the DriverFactory class showing only the createRemoteInstance method
private RemoteWebDriver createRemoteInstance(MutableCapabilities capability) {
RemoteWebDriver remoteWebDriver = null;
try {
String gridURL = String.format("http://%s:%s", configuration().gridUrl(), configuration().gridPort());
remoteWebDriver = new RemoteWebDriver(new URL(gridURL), capability);
} catch (java.net.MalformedURLException e) {
logger.log(Level.SEVERE, "Grid URL is invalid or Grid is not available");
logger.log(Level.SEVERE, String.format("Browser: %s", capability.getBrowserName()), e);
} catch (IllegalArgumentException e) {
@eliasnogueira
eliasnogueira / CreateInstance.java
Last active July 5, 2022 04:47
Code snippet about the local instance creation using Selenium and the Factory pattern
public class DriverFactory {
public WebDriver createInstance(String browser) {
Target target = Target.valueOf(configuration().target().toUpperCase());
WebDriver webdriver;
switch (target) {
case LOCAL:
webdriver = BrowserFactory.valueOf(browser.toUpperCase()).createDriver();
break;
@eliasnogueira
eliasnogueira / BrowserFactory.java
Created July 4, 2022 18:37
Example of the Enum Type with methods inside the constant
public enum BrowserFactory {
CHROME {
@Override
public WebDriver createDriver() {
WebDriverManager.getInstance(DriverManagerType.CHROME).setup();
return new ChromeDriver(getOptions());
}
@eliasnogueira
eliasnogueira / general.properties
Last active June 19, 2022 15:43
Example of the general properties file to run the Selenium tests locally or in a remote machine
# target execution: local or remote
target = local
# browser to use
browser = chrome
# run test into headless mode
headless = true
# initial URL