Skip to content

Instantly share code, notes, and snippets.

Avatar
🤖

Elias Nogueira eliasnogueira

🤖
View GitHub Profile
@eliasnogueira
eliasnogueira / PostUsingHashMapTest.java
Created Mar 20, 2021
Example of an object being used to send a request on RestAssured as a HashMap
View PostUsingHashMapTest.java
class PostTest {
@Test
void postUsingHashMap() {
Map<String, Object> user = new HashMap<>();
user.put("name", "Elias");
user.put("job", "Principal Engineer");
given().
contentType(ContentType.JSON).
@eliasnogueira
eliasnogueira / PostObjectRestAssuredString.java
Created Mar 20, 2021
Example of an object being used in a post request as a String
View PostObjectRestAssuredString.java
class PostTest {
@Test
void postUsingStringConcatenation() {
String user = "{\"name\":\"Elias\",\"job\":\"Principal Engineer\"}";
given().
contentType(ContentType.JSON).
body(user).
when().
@eliasnogueira
eliasnogueira / BaseWeb.java
Created Mar 14, 2021
Example of a BaseTest class using the browser factory approach
View BaseWeb.java
public class BaseWeb {
protected WebDriver driver;
@BeforeEach
public void preCondition() {
String browserToUse = "chrome";
driver = new DriverFactory().createInstance(browserToUse);
@eliasnogueira
eliasnogueira / ChromeDriverManager.java
Created Mar 14, 2021
Driver manager class to create a Google Chrome browser instance
View ChromeDriverManager.java
public class ChromeDriverManager implements Factory {
@Override
public WebDriver createDriver() {
WebDriverManager.getInstance(CHROME).setup();
return new ChromeDriver();
}
}
@eliasnogueira
eliasnogueira / RegularBrowserInstance.java
Last active Mar 14, 2021
Code snippet to show that we could directly use the browser class
View RegularBrowserInstance.java
// previous code ignored
case CHROME:
driver = new ChromeDriver();
break;
@eliasnogueira
eliasnogueira / DriverFactory.java
Last active Mar 14, 2021
Basic implementation of a factory pattern to create browser instances using Selenium WebDriver
View DriverFactory.java
public class DriverFactory {
public WebDriver createInstance(String browser) {
WebDriver driver;
BrowserList browserType = BrowserList.valueOf(browser.toUpperCase());
switch (browserType) {
case CHROME:
driver = new ChromeDriverManager().createDriver();
@eliasnogueira
eliasnogueira / FirefoxTest.java
Created Mar 7, 2021
Selenium WebDriver example using Firefox and it driver
View FirefoxTest.java
public class FirefoxTest {
@Test
public void openPageUsingFirefox() {
System.setProperty("webdriver.gecko.driver", "/path/to/firefox");
WebDriver driver = new FirefoxDriver();
driver.get("http://eliasnogueira.com");
}
}
@eliasnogueira
eliasnogueira / GoogleChromeTest.java
Created Mar 7, 2021
Selenium WebDriver example using Google Chrome and it driver
View GoogleChromeTest.java
public class GoogleChromeTest {
@Test
public void openPageUsingChrome() {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://eliasnogueira.com");
}
}
@eliasnogueira
eliasnogueira / DecodeQRCode.java
Created Feb 11, 2021
Decode an image based either on a full path or base64 content
View DecodeQRCode.java
private static String decodeQRCode(Object qrCodeImage) {
Result result = null;
try {
BufferedImage bufferedImage;
// if not (probably it is a URL
if (((String) qrCodeImage).contains("http")) {
bufferedImage = ImageIO.read((new URL((String)qrCodeImage)));
@eliasnogueira
eliasnogueira / ReadSrcAttribute.java
Created Feb 11, 2021
Reads the image src attribute and decode it using Zxing
View ReadSrcAttribute.java
@Test
void readQRCodeFromURL() {
String qrCodeFile = driver.findElement(By.id("qr")).getAttribute("src");
// get the qr code content and assert the result
String qrCodeResult = decodeQRCode(qrCodeFile);
assertThat(qrCodeResult).isEqualTo("c72a0de5-eba3-4bf0-bde2-fc709e71df29");
}