Advanced logging for your Selenium tests with Silk4J

In the previous posts about Python and Ruby, we have introduced the idea of generating TrueLog files when running Selenium tests through the Silk Test Open Agent. In this post we want generate similar TrueLog files for Java-based and JUnit-based tests. There is an important difference to the previous test types: When using Java and JUnit, you can leverage the full Silk4J tooling and infrastructure, which makes generating TrueLog files for your Selenium scripts very easy.

Overview

The architecture differs to the ones used for Ruby and Python and looks like the following:

The important difference to the other languages is here that you can use the JTF library of Silk Test to start the browser, and you can then interact with the browser through WebDriver.
Note: You can use the JTF library to do a lot more, for example interacting with the native parts of the browser.

Getting started

Start by creating a new Silk4J project for your web application in Eclipse. Select your preferred browser and decline recording for the time being. Instead of recording new actions, import or copy and paste your existing JUnit-based Selenium tests into the new Silk4J project.

If you get compile errors because of the missing WebDriver classes, don't worry. Just use the Add WebDriver Capability option in the Silk4J section of the context menu of the project:

This adds a reference to the WebDriver client library which is shipped with Silk Test, and which is by default installed to C:\Program Files (x86)\Silk\SilkTest\ng\SeleniumJavaClientBindings. If you don't want to use these client bindings, feel free to use Maven, Gradle, or any other dependency management tool.

Code changes

The only major change you have to apply to your existing tests is the way the browser is started. For example, if your tests look like the following:

public class InsuranceWebTests {
  private static WebDriver driver;

  @BeforeClass
  public static void beforeClass() {
    driver = new ChromeDriver();
  }
  
  @AfterClass
  public static void afterClass() {
    driver.quit();
  }
  
  @Before
  public void setup() {
    driver.get("http://demo.borland.com");
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.linkText("Demo Application"))).click();
  }
  
  @After
  public void teardown() {
  }
  
  @Test
  public void testLogin() {
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("login-form:email"))).sendKeys("john.smith@gmail.com");
    driver.findElement(By.id("login-form:password")).sendKeys("john");
    driver.findElement(By.id("login-form:login")).click();
    
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("logout-form:logout"))).click();
  }
  
  @Test
  public void testAgentLookup() {
    WebElement menu = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("quick-link:jump-menu")));
    new Select(menu).selectByVisibleText("Agent Lookup");
    
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("zip-search:zipcode"))).sendKeys("78731");
    driver.findElement(By.id("zip-search:search-zipcode")).click();
    
    WebElement walker = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[text()='Walker']")));
    assertEquals("Walker", walker.getText());
    
    driver.findElement(By.linkText("Home")).click();
  }
}

Simply change the beforeClass method to not directly create a ChromeDriver object, but instead to create a Silk Test BrowserBaseState, to execute that new state, and to retrieve the WebDriver object from the resulting BrowserApplication object:

@BeforeClass
public static void beforeClass() {
  Desktop desktop = new Desktop();
  BrowserBaseState baseState = new BrowserBaseState();
  baseState.setUrl("http://demo.borland.com");
  BrowserApplication app = baseState.execute(desktop);
  driver = app.getWebDriver();
}

That's all! Now you can run your tests through the Run as Silk4J Test… command of the context menu or you can launch configurations in Eclipse, and you will automatically get TrueLog files, including screenshots, if those are turned on in the Options.

Outlook

That was easy! But what if you either don't want to use Eclipse or JUnit but you still have Java-based WebDriver tests for which you want to get TrueLog files? This is also no problem and will be the subject of another blog post, in which we will describe how you can create TrueLog files for TestNG tests that are triggered from IntelliJ.

  • Hi,  Based on my research, web driver capability is introduced only in SILK TEST 18. Can the above be done using SILK TEST 16.5 by  referencing the selenium javaclient bindings which I assume is selenium.-java-xxx.jar?

  • I am able to succeed running the selenium scripts in silk test 16.5 except for being able to display the truelog files

  • Hi!

    Unfortunately this won’t work with Silk Test 16.5, as the Selenium capabilities were introduced with Silk Test 18.0. You can of course run Selenium scripts within the context of your Silk4J project in Silk Test 16.5 as well, but you won’t have the advanced capabilities described in this post.

    Let me know if you have any further questions!