Skip to content

Commit

Permalink
Added better examples of custom steps definitions and updated the gin…
Browse files Browse the repository at this point in the history
…gerspec dependency in pom
  • Loading branch information
jose.fernandez committed Sep 10, 2019
1 parent 3f22ca4 commit 26a4083
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 80 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.github.privaliatech</groupId>
<artifactId>gingerspec-starter</artifactId>
<version>1.5-SNAPSHOT</version>
<version>1.5</version>

<name>Archetype - gingerspec-starter</name>
<description>A maven archetype for creating new automation projects based on gingerspec library.</description>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/archetype-resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


<properties>
<privalia.gingerspec.version>2.0.2</privalia.gingerspec.version>
<privalia.gingerspec.version>2.0.3</privalia.gingerspec.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,69 +1,97 @@
package ${groupId}.${artifactId}.specs;

import com.privalia.qa.specs.*;
import com.privalia.qa.utils.ThreadProperty;
import cucumber.api.java.en.Given;
import org.openqa.selenium.remote.RemoteWebDriver;

/**
* Extending the {@link BaseGSpec} class from the bdt-lib allow us to
* Extending the {@link BaseGSpec} class from the gingerspec allow us to
* define custom step definitions for our project and at the same time
* make use of the properties of the {@link CommonG} object
*/
public class CustomStepsDefinition extends BaseGSpec {

GivenGSpec commonspecGiven;
WhenGSpec commonspecWhen;
ThenGSpec commonspecThen;
BigDataGSpec commonspecBigData;
SeleniumGSpec commonspecSelenium;

BigDataGSpec bigDataGSpec;
FileParserGSpec fileParserGSpec;
KafkaGSpec kafkaGSpec;
RestSpec restSpec;
SeleniumGSpec seleniumGSpec;
SoapServiceGSpec soapServiceGSpec;
SqlDatabaseGSpec sqlDatabaseGSpec;
SshGSpec sshGSpec;
UtilsGSpec utilsGSpec;

/**
* Example how to inherit the needed objects from bdt-lib
* Example of how to inherit the needed objects from gingerspec
* @param spec
*/
public CustomStepsDefinition(CommonG spec) {

/**
* the CommonG object with "atomic" methods and interface to utils files
*/
this.commonspec = spec;

/**
* common given gherkin steps: ssh, rest, etc
*/
commonspecGiven = new GivenGSpec(this.commonspec);
/* Access all functions for working with Big data functionality */
bigDataGSpec = new BigDataGSpec(this.commonspec);

/* Access all functions for handling and parsing text files */
fileParserGSpec = new FileParserGSpec(this.commonspec);

/* Access all functions for working with kafka */
kafkaGSpec = new KafkaGSpec(this.commonspec);

/* Access all functions for working with REST services */
restSpec = new RestSpec(this.commonspec);

/* Access all functions for working with selenium */
seleniumGSpec = new SeleniumGSpec(this.commonspec);

/**
* common when gherkin steps: ssh, rest, etc
*/
commonspecWhen = new WhenGSpec(this.commonspec);
/* Access all functions for working with SOAP web services */
soapServiceGSpec = new SoapServiceGSpec(this.commonspec);

/**
* common then gherkin steps: ssh, rest, etc
*/
commonspecThen = new ThenGSpec(this.commonspec);
/* Access all functions for working with relational databases */
sqlDatabaseGSpec = new SqlDatabaseGSpec(this.commonspec);

/**
* common BigData gherkin steps: Cassandra, MongoDB, elasticsearch, etc
*/
commonspecBigData = new BigDataGSpec(this.commonspec);
/* Access all functions for running bash commands and establishing SSH connections */
sshGSpec = new SshGSpec(this.commonspec);

/**
* new Selenium steps
*/
commonspecSelenium = new SeleniumGSpec(this.commonspec);
/* Access all other useful functions/operations */
utilsGSpec = new UtilsGSpec(this.commonspec);

}


/**
* Step example. You can define your own steps that can be used in the feature files in case
* you have a very specific use case that is not covered in the library
* This is an example of a custom step. You can merge several low-level gingerspec steps into a
* higher-level step just by calling the underlying functions.
*
* To help you with this, execute your tests with -DSHOW_STACK_INFO. This will provide you with information
* about what functions of gingerspec are being called and with what arguments
*
* @throws Throwable Throwable
*/
@Given("^I want to go to disneyland$")
public void mytest() {
RemoteWebDriver d = this.commonspec.getDriver();
d.get("https://www.disney.com");
@Given("^I verify the Interactions and Widgets sections are present$")
public void iVerifyTheInteractionsAndWidgetsSectionsArePresent() throws Throwable {
seleniumGSpec.setupApp("demoqa.com:80");
seleniumGSpec.seleniumBrowse(null,"/");
seleniumGSpec.assertSeleniumNElementExists(2,"class","widget-title");
utilsGSpec.idleWait(1);
}


}
/**
* This is an example of a custom step. You can merge several low-level gingerspec steps into a
* higher-level step just by calling the underlying functions.
*
* to access variables !{} use ThreadProperty.get(variable)
* to access variables ${} use System.getProperty(variable)
*
* @throws Throwable Throwable
*/
@Given("^I verify that a successful response with a valid body is returned$")
public void iVerifyThatASuccessfulResponseWithAValidBodyIsReturned() throws Throwable {
restSpec.setupApp("securely","jsonplaceholder.typicode.com:443");
restSpec.sendRequestNoDataTable("GET","/posts",null,null,null);
restSpec.assertResponseStatusLength(200,null);
restSpec.saveElementEnvironment(null,"$.[0].userId","USER_ID");
utilsGSpec.checkValue(ThreadProperty.get("USER_ID"),"matches","1");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Feature: Testing a RestFull API
And I save element '$.[0].userId' in environment variable 'USER_ID'
Then '!{USER_ID}' matches '1'

Scenario: This is the same scenario as above, but in one line, using custom steps
Given I verify that a successful response with a valid body is returned

Scenario: A new element is inserted via a POST call
Given I securely send requests to 'jsonplaceholder.typicode.com:443'
When I send a 'POST' request to '/posts' based on 'schemas/mytestdata.json' as 'json'
Expand All @@ -37,25 +40,3 @@ Feature: Testing a RestFull API
| $.body | contains | This is a test |
| $.userId | not equal | 2 |

@runOnEnv(SERVER)
Scenario: Setting headers using a datatable
Given I send requests to '${SERVER}'
Given I set headers:
| x-user | myuser |
| x-token | 1234567890 |
When I send a 'GET' request to '/api/v1/shipment/1' as 'json'
Then the service response status must be '200'
And I clear headers from previous request
When I send a 'GET' request to '/api/v1/shipment/1'
Then the service response status must be '401'

@runOnEnv(SERVER)
Scenario: Failed to load resources, no authentication headers
Given I send requests to '${SERVER}'
When I send a 'GET' request to '/api/v1/shipment'
Then the service response status must be '401'
And I save element '$' in environment variable 'response'
And 'response' matches the following cases:
| $.errors[0].title | contains | Forbidden access. |
And 'response' matches the following cases:
| $.errors[0].source | contains | system |
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,12 @@ Feature: Testing basic functionality of a web page
When '2' elements exists with 'class:widget-title'
And I wait '1' seconds

Scenario: Verify button and checkbox with xpath and click on it
Given My app is running in 'demoqa.com:80'
And I browse to '/button'
When '1' elements exists with 'xpath://*[@id="content"]/div[2]/div/input'
And I click on the element on index '0'
And I browse to '/checkboxradio'
When '1' elements exists with 'xpath://*[@id="content"]/div[2]/div/fieldset[1]/label[1]'
And I click on the element on index '0'
And I wait '1' seconds
Scenario: This is the same scenario as above, but in one line, using custom steps
Given I verify the Interactions and Widgets sections are present

Scenario: Write text on a text input
Given My app is running in 'demoqa.com:80'
And I browse to '/autocomplete'
When '1' elements exists with 'id:tags'
Then I type 'Java' on the element on index '0'
And I wait '1' seconds

Scenario: Using a custom step (check CustomStepDefinition class)
Given My app is running in 'demoqa.com:80'
And I browse to '/'
Given I wait '1' seconds
Then I want to go to disneyland
Given I wait '1' seconds
And I wait '1' seconds

0 comments on commit 26a4083

Please sign in to comment.