Skip to content

Instantly share code, notes, and snippets.

View Wait.java
public void forExistence(final By by) {
(new WebDriverWait(driver, AppProps.TIME_OUT))
.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElements(by).size() > 0;
}
});
}
@LenarBad
LenarBad / RetryAnalyzer.java
Last active Nov 14, 2017
How to re-run failed tests in TestNG using IRetryAnalyzer
View RetryAnalyzer.java
import org.joda.time.DateTime;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
public class RetryAnalyzer implements IRetryAnalyzer {
private int retryCount = 0;
private static int maxNumberOfRetriesForUnstableTests = 2;
private static int maxNumberOfRetriesForVeryUnstableTests = 3;
private static int relaxDayMaxNumberOfRetries = 10;
@LenarBad
LenarBad / CloneObject.java
Created Nov 20, 2017
Clone an object in Java
View CloneObject.java
private User testUser;
public User getTestUser() {
Gson gson = new Gson();
return gson.fromJson(gson.toJson(testUser), User.class);
}
@LenarBad
LenarBad / CloneList.java
Last active Nov 20, 2017
Clone lists in Java with Gson
View CloneList.java
private List<User> testUsers;
public List<User> getTestUsers() {
Gson gson = new Gson();
return Arrays.asList(gson.fromJson(gson.toJson(testUsers), User[].class));
}
View Main method class TestNGWithSpringApplication.java
package io.lenar.examples.spring.start;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestNGWithSpringApplication {
public static void main(String[] args) {
SpringApplication.run(TestNGWithSpringApplication.class, args);
@LenarBad
LenarBad / ReadFileIntoString.java
Last active Dec 7, 2017
How to read file into String with Commons IO
View ReadFileIntoString.java
public String readFileIntoString(String fileName) throws IOException {
InputStream input = this.getClass().getResourceAsStream("/" + fileName);
return IOUtils.toString(input, StandardCharsets.UTF_8);
}
@LenarBad
LenarBad / ReadFileIntoListOfStrings.java
Created Dec 8, 2017
How to read file into String with Apache Commons IO
View ReadFileIntoListOfStrings.java
public List<String> getFileAsListOfStrings(String fileName) throws IOException {
InputStream input = this.getClass().getResourceAsStream("/" + fileName);
return IOUtils.readLines(input, StandardCharsets.UTF_8);
}
@LenarBad
LenarBad / ReadFileIntoListOfStringsGuava.java
Created Dec 8, 2017
How to read file into List of Strings with Google Guava
View ReadFileIntoListOfStringsGuava.java
public List<String> getFileAsListOfStringsGuava(String fileName) throws IOException {
InputStream input = this.getClass().getResourceAsStream("/" + fileName);
return CharStreams.readLines(new InputStreamReader(input, StandardCharsets.UTF_8));
}
View Getting test data from json file as a Spring bean.java
@Component
public class TestData extends FileReader {
@Value("${testData.users.file}")
private String usersFile;
public List<User> getUsers() {
Gson gson = new Gson();
return Arrays.asList(gson.fromJson(getFileAsString(usersFile), User[].class));
}
@LenarBad
LenarBad / GET HTTP Request.java
Created Feb 2, 2018
How to send GET Request and get Response with Apache Http Components
View GET HTTP Request.java
String url = "https://www.some-web-service/api/endpoint";
HttpGet get = new HttpGet(url);
Header headers[] = { new BasicHeader("Accept", "application/json")};
get.setHeaders(headers);
CloseableHttpClient client = HttpClients.custom().build();
HttpResponse response = client.execute(get);