~ read.

Easy way of adding logging to your test project

Recently i was working on one of our automation projects using Selenium WebDriver with Java bindings and came to an idea that our execution logs are not really informative. It's pretty nice when you can see in logs some useful data that you can use in further debuging when build fails in Jenkins for example. If you can get an id of failing instance in logs, then you might not need to re-ran tests again tryign to reproduce it. That's what i wanted to achieve and after 5-10 minutes of googling i came to this easy solution. So, we have a Page Object framework and in it we have 2 base classes - TestBase and PageBase. I think you already know what are they for :) So, in each base class i've added :

public static final Logger logger = Logger.getLogger(TestBase.class.getName());

After that i can easily use logging messages in my both tests and page objects. So, basic test with logging would look something like this :

@Test
    public void createWebHosting() throws JSONException, InterruptedException, 
    InvalidArgumentException {
        login = new LoginPage(appManager);
        SearchPage search = login.loginToCas("testUser", "testtest");
        logger.info("Creating new customer");
        NewCustomerPage customerPage = search.clickCreateCustomer();
        String customerNumber = customerPage.createCustomer(
        NewCustomerPage.customerData()).getString("customerNumber");

        logger.info("Customer was created with number : "+customerNumber);
        logger.info("Creating domain for newly created customer: "+customerNumber);
        String domainName = customerPage.goToDomains()
        							    .clickCreateDomain()
								        .createDefaultDomain();

And the output in console during test running would look like this :

May 05, 2014 4:08:21 PM foo.tests.WebHostingTest createWebHosting
INFO: Customer was created with number : 1405841541
May 05, 2014 4:08:21 PM foo.tests.WebHostingTest createWebHosting
INFO: Creating domain for newly created customer: 1405841541
May 05, 2014 4:08:25 PM foo.tests.WebHostingTest createWebHosting
INFO: Domain testDom8203.org was created for customer 1405841541
May 05, 2014 4:08:25 PM foo.tests.WebHostingTest createWebHosting
INFO: Creating webhosting pack for customer1405841541
May 05, 2014 4:08:26 PM foo.pages.WebHostingPage clickCreateNewPack
INFO: Trying to create new WebHosting pack
May 05, 2014 4:08:29 PM foo.tests.WebHostingTest createWebHosting
INFO: WebHosting pack was created succesfully

You can write your logs into the file and also you can redirect error and system output into files as well. For that you can read more here

comments powered by Disqus
comments powered by Disqus