2

I was hoping for some help setting up my test frame work as its causing me issue. Im currently using zalenium in conjunction with my selenium tests. Currently im setting the desired capabilities in the @BeforeTest section of my tests:

@BeforeTest
@Parameters("browser")
public void setup(String br)throws MalformedURLException {


    de = new DesiredCapabilities();
    if  (br.equals("chrome")) {
        de.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
        de.setCapability(CapabilityType.PLATFORM_NAME, org.openqa.selenium.Platform.LINUX);
    }
    else if(br.equals("firefox")){

        de.setCapability(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
        de.setCapability(CapabilityType.PLATFORM_NAME, Platform.LINUX);

    }

    URL url = new URL("http://localhost:4444/wd/hub");

    driver = new RemoteWebDriver(url,de);

    driver.get(URL);

This allows me to run my testing in the docker environment and not on my local machine and is working correctly.

However i would like to create a base for these capabilities so i don't have to keep stating the desired capabilities for each test.

I want to do this also because I would like to set up separate classes for each page. Currently when i try this im getting a null pointer exception because the driver isnt declared. I tried to inject the Remote webdriver like so:

@Test
public void loginTest( RemoteWebdriver driver){


    WebElement user_account_menu = driver.findElement(By.cssSelector("[data-user-account-settings-button]"));

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.elementToBeClickable(user_account_menu));

    user_account_menu.click();
    System.out.println("Login clicked successfully");

}

Im receiving the error: Cannot inject @Test annotated Method [loginTest] with [class org.openqa.selenium.remote.RemoteWebDriver

So im basically trying to figure out how i can set up these capabilities for the driver in a class and then extend them onto my tests.

Pankaj Devrani
  • 510
  • 1
  • 10
  • 28
tester1986
  • 105
  • 1
  • 1
  • 9
  • Are you using the JUnit testing framework? – natn2323 Nov 25 '19 at 15:41
  • I'm using TestNG, for Zalenium the tutorial suggested a TestNG xml file to run all my tests. All I really want to do is call from different package so i dont have to keep declaring different web elements – tester1986 Nov 25 '19 at 16:04

1 Answers1

2

From this link, it appears, you're not adding the right annotation, namely @Parameters.

However, in some other testing frameworks, it's not typical to be able to pass in variables that aren't determined at runtime, i.e. variables for objects like RemoteWebdriver won't work, but variables for strings or ints will work. From this link, it appears that what you're trying to accomplish is doable. But I recommend the following approach.

Have an enumeration:

enum BrowserType
{ 
    Chrome, Firefox, IE;
}

Have your test base page:

public class BrowserSetup
{
    public static RemoteWebdriver Initialize(string browserType)
    {
        switch (browserType)
        {
            case BrowserType.Chrome:
                // set chrome capabilities and initialize your browser here
                break;
            case BrowserType.Firefox:
                // set firefox capabilities and initialize your browser here
                break;
            default:
                // set default case, or fail if browser type doesn't match
                break;
    }
}

Then, from you're test class, assuming you have a private RemoteWebdriver driver initialized somewhere in the test class, you can do the following:

@BeforeTest
@Parameters("Chrome")
public void setup(String browserType) throws MalformedURLException 
{
    driver = BrowserSetup.Initialize(browserType);
}
natn2323
  • 1,983
  • 1
  • 13
  • 30