Sergey Pirogov
Sergey Pirogov
Senior Software
Test Automation Engineer
EPAM Systems
skills: ['Java' , 'Groovy']
blog: http://automation-remarks.com
twitter: @s_pirogov
conference: QA Fest, SQA days, SeleniumCamp
Boilerplate
We live in dynamic world
We test dynamic web applications
Time to use dynamic languages!
assertEquals(person.getAccountName(accounNumber), "AC1220");
org.junit.ComparisonFailure:
Expected :AC[20]0
Actual :AC[122]0
assert person.getAccountName(accounNumber) == "AC1220"
Caught: Assertion failed:
assert person.getAccountName(accounNumber) == "AC1220"
| | | |
| AC200 | |
| 100 false
Person("Ivan", 12)
check {
softly { assert frodo.name == "Frodo" }
softly { assert frodo.race == "Hobbit" }
}
SoftAssertions softly= new SoftAssertions();
softly.assertThat(frodo.getName()).isEqualTo("Frodo");
softly.assertThat(frodo.getRace()).is("Hobbit");
softly.assertAll();
<response version-api="2.0">
<value>
<books>
<book available="20" id="1">
<title>Don Xijote</title>
<author id="1">Manuel De Cervantes</author>
</book>
<book available="14" id="2">
<title>Catcher in the Rye</title>
<author id="2">JD Salinger</author>
</book>
<book available="13" id="3">
<title>Alice in Wonderland</title>
<author id="3">Lewis Carroll</author>
</book>
<book available="5" id="4">
<title>Don Xijote</title>
<author id="4">Manuel De Cervantes</author>
</book>
</books>
</value>
</response>
org.jdom.input.SAXBuilder saxBuilder = new SAXBuilder();
try {
org.jdom.Document doc = saxBuilder.build(new StringReader(xml));
String message = doc.getRootElement().getText();
System.out.println(message);
} catch (JDOMException e) {
// handle JDOMException
} catch (IOException e) {
// handle IOException
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
try {
Document doc = db.parse(is);
String message = doc.getDocumentElement().getTextContent();
System.out.println(message);
} catch (SAXException e) {
// handle SAXException
} catch (IOException e) {
// handle IOException
}
} catch (ParserConfigurationException e1) {
// handle ParserConfigurationException
}
def books = new XmlSlurper().parseText(xml)
<book available="20" id="1">
<title>Don Xijote</title>
<author id="1">Manuel De Cervantes</author>
</book>
def title = books.book.find { it.author.@id == 1 }.title
Map<Integer,Person> map = new HashMap<>();
map.put(1, new Person("Ivan", 23));
map.put(2, new Person("Dima", 12));
map.put(3, new Person("Vlad", 34));
map = [1: new Person("Ivan", 23), 2: new Person("Dima", 12), 3: new Person("Vlad", 34)]
Map<Integer, Person> persons =
map.entrySet()
.stream()
.filter(m -> m.getValue().getAge() > 18)
.collect(Collectors.toMap(m -> m.getKey(), m -> m.getValue()));
map.findAll { it.value.age > 18 }
Filter Map
Groovy way
browser = 'firefox'
emails = [from: 'director', to: 'accountant']
environments {
dev {
host = "devthost"
}
test {
host = "testhost"
browser = 'ie'
}
}
Config.groovy
This is CODE
def config = new ConfigSlurper("dev").parse(Config)
assert config.host == "devhost"
assert config.browser == "firefox"
assert config.emails.from == "director"
assert config.emails.to == "accountant"
Flexible and elegant
https://github.com/aestasit/sshoogr
@Grab('com.aestasit.infrastructure.sshoogr:sshoogr:0.9.18')
import static com.aestasit.infrastructure.ssh.DefaultSsh.*
remoteSession('user2:654321@localhost:2222') {
exec 'rm -rf /tmp/*'
exec 'touch /var/lock/my.pid'
remoteFile('/var/my.conf').text = "enabled=true"
def file = "/root/home/test.txt"
def selenium = "selenium-server-standalone-2.48.2.jar"
//read from file
print remoteFile(file).text
//copy file to remote host
scp {
from { localFile "/root/home/${selenium}"}
into { remoteFile "/root/remote/${selenium}" }
}
}
public class MainPage extends PageObject {
@FindBy(linkText = ".all")
WebElement allbooksButton;
@FindBy(linkText = ".search")
WebElement searchButton;
@FindBy(name = "query")
WebElement searchField;
@FindBy(css = "button")
WebElement searchBegin;
public BookPage(WebDriver driver) {
super(driver);
}
class MainPage extends Page {
}
static url = "http://ukr.net"
static content = {
loginInput {$ ".loginForm input"}
passwordInput { $ ".password input"}
submit {$ ".submit button"}
error {$ ".error-text"}
}
http://automation-remarks.com/groovy-page/
//DeclareComponent
class LoginForm extends Page {
static content = {
loginInput {$ ".loginForm input"}
passwordInput { $ ".password input"}
submit {$ ".submit button"}
error {$ ".error-text"}
}
def login(String name,String password){
loginInput << name
passwordInput << password
submit.click()
}
}
class MainPage extends Page {
@Component
LoginFrom loginForm
}
Page object become tiny
Team efficiency increased twice
We stoped writing boilerplate
Readable and maintainable tests
def "shouldBeLoginError"() {
MainPage mainPage = go MainPage
mainPage.loginForm.login("test","test")
assert mainPage.loginForm.errorText == "Wrong credentials"
}
twitter: @s_pirogov
blog: http://automation-remarks.com