Currently Browsing

Selenium

Cucumber in Web testing (part 8 of 8)

8. Keyword “Examples” and conclusion

 

Gherkin keyword: Examples

Cucumber provides another useful Gherkin keyword which is used scenario parametrization. A scenario with an “Example” keyword will be executed multiple times with different parameters. Here is an instance:

@init @logout
  Scenario Outline: Successful Login with Valid Credentials
    Given User is on Home Page
    When User Navigate to LogIn Page
    And User enters username "<userName>"
    And User enters password "<password>"
    And Click login
    Then Message displayed Login Successfully

 
Examples:
| userName | password |
| username1 | password 1 |
| invalid | invalid |

Cucumber in Web testing (part 7 of 8)

7. Hooks

 

Cucumber provides a methodology called Hooks which is analogue to traditional before and after methods. The hooks must be annotated with @Before and @After in step classes. Make sure to use cucumber.api.java.Before and cucumber.api.java.After annotations and not the JUnit ones. By default each before method will be executed before all scenarios and each after method will be executed after all scenarios. If a hook needs to be executed only for a particular scenario, then we need to use tags. To tag a before method, we need to insert a string parameter to the annotation. To bind the tagged hook to a particular scenario we need to “annotate” the scenario with the same string tag. Here is an example:

@Before("@login")
	public void login() throws Throwable{
		openDriver();
		System.out.println("login()");
		testSteps.user_is_on_Home_Page();
		testSteps.user_Navigate_to_LogIn_Page();
		testSteps.user_enters_UserName("Tester843");
		testSteps.user_enters_Password("x%kzJwbsW1)AIB2k");
		testSteps.click_login();
		testSteps.message_displayed_Login_Successfully();
	}
@After("@close")
	public void close_driver() throws Throwable {
		System.out.println("close_driver()");
		baseObject.driver.close();
	}

@login @close
 Scenario: Successful LogOut
    When User LogOut from the Application
    Then Message displayed LogOut Successfully

Cucumber in Web testing (part 6 of 8)

6. Page flow

 

Page flow is a widely used pattern in web testing which describes how we should use our page objects to write more readable and well designed code. What page flow telling us, is that in our test code every Page Object method which navigates from one page to another should return a new Page Object. The following actions in the test code will be executed by calling methods from the newly returned Page Object. Here is an example:
 
Sample code - Password change
 

The benefits of this pattern are:

  1. Easier to write test code. The Page Object methods reveals which page will be the next the method execution.
  2. Improving test code readability
  3. We got additional verifications, because each page object extends the Selenium’s LoadableComponent class. When we are creating a new instance of a PageObject the isLoaded() method will be executed, which contains validations against must displayed elements on the page.

Cucumber in Web testing (part 5 of 8)

5. Web testing project structure

 

Structure without cucumber

The following picture represents our basic structure what we use for web testing:

 
TestBase contains the initialization and termination of a Selenium Webdriver. It usually contains a before() and after() method where we start and close web browser. In addition to this we setting up desired capabilities if needed.
 

Cucumber in Web testing (part 4 of 8)

4. TestRunner and Test steps

To run features we need a Java class to start the test run. Here is a simple example:

package cucumberTest;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
features = "Feature",
dryRun = true
)

public class TestRunner {

}

The TestRunner class needs to be annotated with @RunWith(Cucumber.class) and a body of it can be totally empty. By annotating the class with @CucumberOptions we can specify extra options to our test run. In this case, we used the annotation to specify the location of the feature file and to set the test run to ‘dry’. Running the test in ‘dry’ mode means that cucumber will not execute the test steps, rather it will just check for test steps implementations. For the previously given feature, we got this output on the console.

How to check presence of an element when it disappears in less than 500ms

It can happen that the element you want to check only appears for a fraction of a second. This can produce false failures when the element appears in between the standard 500ms when WebDriver checks the wait condition. What you can do is to set up a FluentWait object with a specific polling time. It works almost the same as other kinds of wait functions in WebDriver.

The code is the following:



	Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
				.withTimeout(20, java.util.concurrent.TimeUnit.SECONDS)
				.pollingEvery(200, java.util.concurrent.TimeUnit.MILLISECONDS)
				.ignoring(NoSuchElementException.class);
		
		Function<WebDriver, WebElement> function = new Function<WebDriver, WebElement>() {
			
			@Override
			public WebElement apply(WebDriver driver) {
				return driver.findElement(exportSnackbarCss);
			}		
		};
		
		wait.until(function);

Cucumber in Web testing (part 3 of 8)

3. Cucumber features

“Feature” in cucumber is supposed to describe a single feature which is analog with “test group”/”test class” in standard Java world. A feature includes one or more scenarios. A scenario can be represented as a “test case” or a ”test method“ in standard Java world. The code of a cucumber feature need to be placed in a file with “.feature” extension and must be located in a separated folder on the project root.
Creating a new feature file with the previously installed eclipse extension will include a small legend and a simple template for the feature file.

Most important Gherkin keywords are:

Cucumber in Web testing (part 2 of 8)

2. How to install cucumber in Eclipse

To start using cucumber in eclipse we need to install few things:

  1. First of all eclipse and Java Development Kit (JDK) need to be installed on the computer.
  2. Ones eclipse finished with the installation we are able to install “Cucumber Eclipse Plugin” with eclipse built-in plugin installation tool. Step by step instructions can be found here.
  3. Download cucumber libraries. Multiple solution can be found here. We recommend to use Maven to download and manage all dependencies.

Our pom.xml contains the following dependencies:

What is the best site or where to find quality material for learning?

New Year. New Resolution(s). If you haven’t started yet to work on new skills, it is the right period to do it. (it is never late anyway, but sooner is better)
Some holidays are over, some will just arrive. You have already started thinking about “upgrade” yourself to a “bigger”, smarter, more valuable you. Good thinking.

But, the most important question is where to start?

Browsing through the internet you can find a dozen of quality materials. But how can I know where could I learn the most and spend much less time with it?
The question will never have an exact answer because it always depends only on you. You can find quality materials, with the best ratings (actually, those are definitely worth to try), but it will not guarantee that it will fit for you.

So, where to find it?

If you already found earlier some lecturers, presenters, teachers who had shown nice approach to you, by explaining and presenting the materials, follow them. Search for their further work. That will surely spare you from some headache and from spending too much time for searching. Also, if there is one, there will be more. Investigate, listen and watch other ones.
What If I don’t want to spend too much money or I don’t want to spend money at all to those materials?
There are always individuals, who will catch your attention, and just from fun started to share their materials to help anyone who has the will to learn more. You have then three solutions.

Lambda expressions in java 8

One of the key elements of the functional programing is that we can treat functions as objects. In a typical functional language we can have a variable with type of function. The benefits are that we can use functions as:

  • • parameter
  • • return type
  • • variable

The type Function in Java is interface with one abstract method. If we want to be sure that an interface can be used as a function type we can add annotation @FunctionalInterface before the header of the interface. It will throw compilation error if the interface contains more then one abstract method.

If a method has a parameter with function type we can use lambda expression instead of creating an anonymous class. Lambda expression represents an anonymous class in a shorter and readable way. Lambda expression consists from parameter definition, small right arrow “->”, and the body of the method.