IMPORTANT NOTICE:  As of 21 February, anything posted in this community WILL NOT BE MIGRATED to our new community site.   We have pulled all user information and data from this site and are now in an approximately week long process of importing users, roles, and data to our new site.  We are leaving this site open and active so you can post and hopefully get a response until the migration is complete.   Once complete, the URL  that currently takes you to this site will take you to our new site so your bookmarks will work as always.  Please read the information on a New Login Process

Keyword-driven testing with Selenium

Simplify your Selenium test creation with keyword-driven testing. Save time and maintenance by building up your test designs so that keywords can be reused across multiple testing scenarios. Allow business stakeholders to create tests independently from implementation and start testing as early as possible in your Software Development Life Cycle (SDLC). 

Silk Central provides a web interface to create keyword-driven tests without the need to install any additional tool. 

Advantages of Keyword-Driven Testing

The advantages of using the keyword-driven testing methodology are the following:

  • Keyword-driven testing separates test automation from test case design, which allows for better division of labor and collaboration between test engineers implementing keywords and subject matter experts designing test cases.
  • Tests can be developed early, without requiring access to the application under test, and the keywords can be implemented later.
  • Tests can be developed without programming knowledge.
  • Keyword-driven tests require less maintenance in the long run. You need to maintain the keywords, and all keyword-driven tests using these keywords are automatically updated.
  • Test cases are concise.
  • Test cases are easier to read and to understand for a non-technical audience.
  • Test cases are easy to modify.
  • New test cases can reuse existing keywords, which amongst else makes it easier to achieve a greater test coverage.
  • The internal complexity of the keyword implementation is not visible to a user that needs to create or execute a keyword-driven test. 

Create a library

Before you can start with keyword-driven testing, you need a library which will hold your keywords that can be shared across tests. Select Libraries from the Tests menu and create a new library. 

Start writing a keyword-driven test

To create the first keyword-driven test:

  1. Click Tests in the menu and create a new test container
  2. Select the new container and click "New Child Test" in the toolbar
  3. Specify a name and select "Keyword-Driven Test" from the Type list
  4. Select the previously created library and click "Finish" 

After saving the test, select the Keywords tab and start writing your test. This may look like:

  

The orange exclamation marks indicate that there is no automation code associated with the keyword. 

Implement keywords with Selenium using Java

To implement the draft keywords: 

  1. Select Help > Tools and download the Java Keyword-Driven Library Builder.
  2. Add the downloaded com.borland.silk.keyworddriven.jar to the classpath of your Selenium project.
  3. Create a Java class that will contain your keywords.
  4. Create Java methods called as the keywords in your test. The methods need the same number of parameters as the keyword.
  5. Annotate your methods with the annotation com.borland.silk.keyworddriven.annotations.Keyword from the downloaded jar. 

The Selenium implementation for the keywords of the tests shown in the previous section may look as follows: 

public class Keywords {

  private WebDriver driver;  

  @Keyword
  public void openBrowser(String url) {
    driver = new FirefoxDriver();
    driver.get(url);
  }

  @Keyword
  public void login(String email, String password) {
    driver.findElement(By.xpath(".//*[@id='login-form:email']")).sendKeys(email);
    driver.findElement(By.xpath(".//*[@id='login-form:password']")).sendKeys(password);
    driver.findElement(By.xpath(".//*[@id='login-form:login']")).click();
  }

  @Keyword
  public void checkLoggedInAs(String fullName) {
    Assert.assertEquals(fullName, driver.findElement(By.xpath(".//*[@class='login']")).getText());
  }

  @Keyword
  public void closeBrowser() {
    driver.quit();
  }

} 

Generate a library and upload to Silk Central

To generate a keyword-driven library:

  • Compile your sources and create a Java archive (jar).
  • Call the DocBuilder class that is contained in the downloaded jar file with the following arguments:
    • Library name to be generated
    • Java (fixed value)
    • Binaries path where your keyword library jar file resides and if applicable, any other required libraries
    • Name of the output library package (zip archive) to be generated

The following example outlines the command line to execute the DocBuilder: 

java -cp ./com.borland.silk.keyworddriven.jar com.borland.silk.keyworddriven.library.docbuilder.DocBuilder MyLibrary Java ./output ./MyLibrary.zip

 Note: Check the Silk Central online help for the command if you are using a newer version than 16.0. 

To upload your library to Silk Central:

  • Select Libraries in the Tests menu.
  • Select the library you have created previously.
  • Upload the generated zip file on the Properties tab . 

When jumping to your previously created keyword-driven test, you will see that the orange markers are gone and your test is ready for execution. 

Execute keyword-driven tests and Cross-Browser testing

Please check the previous blog to see how:

  • To schedule your keyword-driven tests for execution
  • To execute simultaneous cross-browser tests
  • To get a powerful result analysis including
    • Videos of each test run
    • Screenshots of the application when a test fails
    • Log files

Structuring your keyword library

Once your keyword library starts to grow you need to bring some structure into your library. Split up your keywords into different classes and apply the annotation @KeywordGroup("Groupname") to each of these.

The annotation for the class containing keywords related to your main page might look like: 

  @KeywordGroup("MainPage")
  public class MainPageKeywords

The keyword-driven test editor will show these groups, which can also be used for filtering: 

Keyword sequences

A keyword sequence is a keyword that is a combination of other keywords, which are often executed together. Keyword sequences are also stored in the library, which allows you to better structure your keywords, reducing maintenance effort and keeping your tests well-arranged. 

Enhanced naming

To use characters for your keyword name that are not allowed in Java methods, you can add a String argument to your annotations. This works for keyword names as well as for parameter names. Here is a sample using these arguments:

  @Keyword("My Keyword")
  public void myKeyword(@Argument("User name") String name) {
    System.out.println(name);
  }

Parameters

Input parameters

In Silk Central parameters are freely configurable values that can be used as input for your keywords. Let's assume you want to execute the test of the previous section against different test systems, using different URLs.  Therefore we have to specify a parameter name for the openBrowser keyword instead of an explicit URL. Parameters are specified using the following notation: ${parametername} 

For our sample this would look like:

  

Now you can execute the same test against different systems by simply passing the URL as parameter. 

Output parameters

Sometimes it is necessary to get a value from one keyword and use it in a following keyword. By default a parameter of a keyword method is an input parameter. To define an output parameter, use the class OutParameter for the method parameter.

 The code of a keyword returning the number of rows in a grid might look like this: 

  @Keyword
  public void getNumberOfRowsInGrid(OutParameter nrOfRows) {
    ...
    nrOfRows.setValue(123);
  } 

The keyword-driven test editor in Silk Central shows output parameters with a different background colour: 

In the sample above the first row assigns the returned value to a parameter called numberOfRows. The following keywords can then use this parameter as normal input parameter. 

Trial

A free 45 day product trial can be downloaded at:

http://www.borland.com/Products/Software-Testing/Test-Management/Silk-Central/Product-Trial

  • Hello

    This is an excellent piece of information on selenium key word driven testing.  I have a question on this hoping you would be able to help me out.

    I am currently using SilkCentral 16.5

    I have created a selenium project with keywords and uploaded the keyword library from eclipse silk central configuration capability. I am successfully able to run the test as in the web driver has opened the browser and finished all steps....

    BUT....silk central is unable to identify the job completion and keep showing Running <Test name>

    From the above:

    1. Doesnt seem like you are using junit anywhere...if so...can you help me know which version of junit silk central 16 uses

    2. How does silk central know job has finished? when the last keyword has completed and entire test has no errors?

    3. Does the selenium tests needs to be run as junit test type even though underlying its keyworddriven java test case methods?

    any help would be highly appreciable as I am spending days together to understand the silk central test case statuses.

  • Hi,

    1. Silk Central is shipped with JUnit 4.11

    2. When the execution of the last keyword returns the test is finished. If a keyword threw an error the test will show up as failed.

    3. If you are using the keyword-driven feature in Silk Central the tests will have the type "keyword-driven". This is the type where you can specify the steps of the test using the keywords of the uploaded library.

    If you want you can also create normal JUnit tests driving Selenium, but then you can't use the keyword-driven editor in Silk Central. In this case the test needs to be configured as JUnit test in Silk Central.

    It seems that in your case something hangs and avoids that the test process can exit.

    Can you try the Keywords code from the blog, upload the library and create a simple test that just calls the keywords "openBrowser" and "closeBrowser" or just call a keyword that writes to the System.out. Execute the test and see if this is also hanging.

    Best Regards,

    Sebastian