Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/test/java/skeleton/ListSteps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package skeleton;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import java.util.ArrayList;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Created by korobitsyna on 5/24/15.
*/
public class ListSteps {

private ArrayList<String> testList;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI
Here, usage of ArrayList as an argument type might be correct because we are testing this Java's class.
Usually it's always better to use interface List rather than ArrayList:
private List testLst;

private String testString = "String";

@Given("I have an empty list")
public void i_have_an_empty_list() {
testList = new ArrayList<String>();
}

@Given("I have a list with (\\d+) elements")
public void i_have_a_list_with_elements(int quantity) {
testList = new ArrayList<String>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIY
It's better to call i_have_an_empty_list rather directly create new list. Of course, here, it's trivial constructor. Usually, when we work with more complex object it;s better to use one method to initialize initial state in similar way.
Moreover. Sometimes it's better to place such things into @before methods to simplify test scenarios. However here it's not needed.

for(int i = 0; i < quantity; i++) {
testList.add(testString);
}
}

@When("I add string \"(.*?)\" to a list")
public void i_add_string_to_list(String s) {
testList.add(s);
}

@When("I remove last element")
public void i_remove_element() {
testList.remove(testList.size()-1);
}

@Then("List length is (\\d+)")
public void list_length(int length) {
assertEquals(length, testList.size());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI
It's always better to use assertEquals(msg,expected, actual);
E.g. here
assertEquals("Array list size is different", length, testListSize);

}

@Then("List contains string \"(.*?)\"$")
public void list_contains_string(String s) {
assertTrue(testList.contains(s));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI
Try to avoid using assertTrue or assertFalse in real test scenarios. It's not informative. Here we expect true/false from the API method - so it's ok.

}

}
45 changes: 44 additions & 1 deletion src/test/java/skeleton/RestSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.json.JSONArray;
import org.json.JSONObject;
import sun.misc.IOUtils;

Expand All @@ -20,6 +21,7 @@
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* @author Alexey Lyanguzov
Expand All @@ -46,7 +48,7 @@ public void sum_must_be(int sum) {

@Given("^I have no users$")
public void i_have_no_users() {
doRequest(Unirest.delete(usersBaseUrl));
i_delete_all_users();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good!

}

@When("^I add users$")
Expand All @@ -60,12 +62,53 @@ public void i_add_users(List<User> users) {
}
}

@When("I delete user (.*?)$")
public void i_delete_user(String email) {
doRequest(
Unirest.delete(usersBaseUrl).queryString("email", email)
);

}

@When("I delete all users")
public void i_delete_all_users() {
doRequest(Unirest.delete(usersBaseUrl));
}

@When("I edit user (.*?) with (.*?) and (.*?)$")
public void i_edit_user(String email, String newFirstname, String newLastname) {

doRequest(
Unirest.post(usersBaseUrl + "/edit")
.queryString("email", email)
.body("{\"firstName\":\"" + newFirstname + "\",\"lastName\":\"" + newLastname + "\"}")
.getHttpRequest()
);

}

@Then("^there are (\\d+) users$")
public void there_are_users(int expectedUsersCount) {
doRequest(Unirest.get(usersBaseUrl));
int actualUsersCount = response.getBody().getObject().getJSONArray("users").length();
assertEquals("BBB!", expectedUsersCount, actualUsersCount);
}

@Then("User (.*?) has firstname (.*?) and lastname (.*?)$")
public void user_has_credentials(String email, String firstname, String lastname) {
doRequest(Unirest.get(usersBaseUrl));
JSONArray users = response.getBody().getObject().getJSONArray("users");
int index;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int index is excess.
use for(int index = 0; ....

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can compare within the 'for' loop just after you have find a user.

for(index = 0; index < users.length(); index++) {
JSONObject userRecord = users.getJSONObject(index);
if (userRecord.getString("email").equals(email)) break;

}
String actualFirstname = users.getJSONObject(index).getString("first_name");
String actualLastname = users.getJSONObject(index).getString("last_name");

assertTrue(actualFirstname.equals(firstname) && actualLastname.equals(lastname));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such assertTrue is not good practice. It's not clear what has really happened.
Better solution 1 is to have two assertions:
assertEquals(firstname, actualFirstName);
assertEquals(lastname, actualLastName);

Better than better solution 1 is better solution 2. It requires some additional work but I'll always advocate for using such an approach.
There is object User. Method equals' (and therefore 'hashcode') might be updated. 1 more constructor with parameter JSONObject should be added. So code will look something like:
User expectedUser = new User(email, expectedFirstName, expectedLastName);
Map<String, User> userMap = new HashMap<String, User>();
for(index = 0; index < users.length(); index++) {
JSONObject userRecord = users.getJSONObject(index);
userMap.put(userRecord.getString("email"), userRecord);
}
User actualUser = userList.get(email);
assertEquals("User has wrong credentials", expectedUser, actualUser);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solution 2 might look a little bit complicated but it's the best one when you have dozens of tests. I can show how code might actually look late if you are interesting. (Now I do not have IDE and have only HTTP access to github).

}

private void doRequest(HttpRequest request){
response = null;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/skeleton/RunCukesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty"}, tags = {"~@my"})
@CucumberOptions(plugin = {"pretty"}, tags = {"@my"})
public class RunCukesTest {
}
51 changes: 48 additions & 3 deletions src/test/java/skeleton/SeleniumSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
Expand All @@ -23,6 +25,7 @@ public class SeleniumSteps {
private static final String BASE_URL = "http://localhost:9944/web";
private static final String HOME_PAGE_URL = BASE_URL + "/home";
private static final String ABOUT_PAGE_URL = BASE_URL + "/about";
private static final String LIST_USER_PAGE_URL = BASE_URL + "/listusers";
private static final int LOAD_TIMEOUT = 5;
private static final int WAIT_TIMEOUT = 2;
public static final int DEMO_TIMEOUT = 1000;
Expand Down Expand Up @@ -70,9 +73,9 @@ public void back_Button_is_shown() throws Throwable {
@When("^I try to return back$")
public void i_try_to_return_back() throws Throwable {
WebElement btn1 = driver.findElement(By.id("btn1"));
btn1.click(); //comment this line and uncomment next two
// btn1.sendKeys(Keys.RETURN);
// demo_delay();
//btn1.click(); //comment this line and uncomment next two
btn1.sendKeys(Keys.RETURN);
demo_delay();
}

@Then("^text '([^']+)' is shown$")
Expand All @@ -90,4 +93,46 @@ private void demo_delay(){
}
}
}

@When("I go to List User page")
public void i_open_list_user_page() throws Throwable {
WebElement listUserLink = driver.findElement(By.linkText("list users"));
listUserLink.click();
}

@Then("List User page is opened")
public void list_user_page_is_opened() throws Throwable {
String actualUrl = driver.getCurrentUrl();
assertEquals(LIST_USER_PAGE_URL, actualUrl);
demo_delay();
}

@Then("User table contains (\\d+) records")
public void user_table_contains_records(int number) {
WebElement table = driver.findElement(By.tagName("table"));
List<WebElement> tableContents = table.findElements(By.tagName("tr"));
assertEquals(number, tableContents.size() - 1);
}

@When("I reload the page")
public void reload_page() {
driver.navigate().refresh();
}

@Then("User ([^']+) is on the list")
public void user_is_on_the_list(String email) {
WebElement table = driver.findElement(By.tagName("table"));
List<WebElement> tableContents = table.findElements(By.tagName("tr"));

boolean containsString = false;
ListIterator<WebElement> iter = tableContents.listIterator();
while(iter.hasNext()) {
if(iter.next().getText().contains(email)) {
containsString = true;
break;
}
}
assertTrue(containsString);

}
}
17 changes: 17 additions & 0 deletions src/test/resources/skeleton/api_test_hw.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: test java.util.ArrayList

@my
Scenario: empty list length
Given I have an empty list
Then List length is 0
@my
Scenario: add element to list
Given I have an empty list
When I add string "abc" to a list
Then List contains string "abc"
@my
Scenario: remove element from list
Given I have a list with 5 elements
When I remove last element
Then List length is 4

36 changes: 36 additions & 0 deletions src/test/resources/skeleton/rest_test.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,39 @@ Feature: test through Rest
| u1@a.b | aa | bb |
| u2@c.d | cc | dd |
Then there are 2 users

#Homework test scenarios
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@my
Scenario: delete a user
Given I have no users
And I add users
| email | firstName | lastName |
| test1@mail.com | test1FirstName | test1LastName |
| test2@mail.com | test2FirstName | test2LastName |
When I delete user test1@mail.com
Then there are 1 users
@my
Scenario: delete all users
Given I add users
| email | firstName | lastName |
| test3@mail.com | test3FirstName | test3LastName |
| test4@mail.com | test4FirstName | test4LastName |
When I delete all users
Then there are 0 users

@my
Scenario Outline: editing users
Given I have no users
And I add users
| email | firstName | lastName |
| <email> | <oldFirstname> | <oldLastname> |
When I edit user <email> with <newFirstname> and <newLastname>
Then User <email> has firstname <newFirstname> and lastname <newLastname>

#test valid editing, test editing with empty fields
Examples:
| email | oldFirstname | oldLastname | newFirstname | newLastname |
| test1@mail.com | oldfirstname1 | oldlastname1 | newf1 | newl1 |
| test1@mail.com | oldfirstname1 | oldlastname1 | | |


30 changes: 29 additions & 1 deletion src/test/resources/skeleton/selenium_test.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,32 @@ Feature: Selenium WebDriver
When I try to return back
Then text 'Wow! You have clicked me!' is shown
And About page is still opened



#Homework test scenarios
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@my
Scenario: list users
Given I have no users
And I add users
| email | firstName | lastName |
| test1@mail.com | test1FirstName | test1LastName |
| test2@mail.com | test2FirstName | test2LastName |
And I am on home page
When I go to List User page
Then List User page is opened
And User table contains 2 records

@my
Scenario: list empty user list
Given I have no users
And I am on home page
When I go to List User page
Then List User page is opened
And User table contains 0 records
When I add users
| email | firstName | lastName |
| test1@mail.com | test1FirstName | test1LastName |
And I reload the page
Then User table contains 1 records
And User test1@mail.com is on the list