Selenium Java - How to outsource @Config and call tests from an extern class?

415 Views Asked by At

I'm sure, this question can be answered very quickly by a experienced Java-developer. But as I am not that familiar with Java I don't get how to source out the @Config part of Selenium in Java. It would be optimal, if I could have a config-file or -class where I can put the data (browser, website etc.) on the one hand and on the other hand the test-files.
Here is an example of a test file:

package com.example_test.selenium;

import io.ddavison.conductor.Browser;
import io.ddavison.conductor.Config;
import io.ddavison.conductor.Locomotive;
import org.junit.Test;

@Config(
        browser = Browser.CHROME,
        url     = "http://example.com"
)

public class test_a_Home extends Locomotive {
    @Test
    public void testifExists() {
        validatePresent(site_a_Home.EL_NEWCUSTOMERBANNER);
    }
}

Now I would like to have a seperate file called tests.java where I can call the "test_a_Home"-function. If I try it just with

package com.example_test.selenium;

public class tests {
    test_a_Home test = new test_a_Home();

    test.testifExists();

}

I am receiving the error, that "testifExists()" cannot be resolved.
I tried changing the public void testifExists() to public int testifExists() and tried to call it with int res = test.testifExists(); in the class tests but this does not work either, as I receive the error java.lang.Exception: Method testNewCustomersBannerExists() should be void.
I would be very happy, if anyone could help me. If you need more information please feel free to mention it. Thank you.

1

There are 1 best solutions below

1
ddavison On BEST ANSWER

If you want your design to be like this, then you need to organize your tests as such:

public class BasePage {
    public Locomotive test;
    public BasePage(Locomotive baseTest) {
        test = baseTest;
    }
}

public class test_a_Home extends BasePage {
    public test_a_Home(Locomotive baseTest) {
        super(baseTest);
    }

    public void testifExists() {
        test.validatePresent(site_a_Home.EL_NEWCUSTOMERBANNER);
    }
}

Then your test class, i'd recommend creating a base class as well:

@Config(
    browser = Browser.CHROME,
    url     = "http://example.com"
)
public class BaseTest extends Locomotive {}

And then your test class:

public class tests extends BaseTest {
    test_a_Home test = new test_a_Home(this);

    @Test
    public void testHomePage() {
        test.testIfExists();
    }
}

Also you state state:

I don't get how to source out the @Config part of Selenium in Java.

Please make sure you know, that using Conductor abstracts you from the Selenium API.. It just wraps it. @Config does not belong to Selenium, it belongs to Conductor.