Search in sources :

Example 1 with SeleniumGridConnector

use of com.seleniumtests.connectors.selenium.SeleniumGridConnector in project seleniumRobot by bhecquet.

the class TestTasks method executeCommand.

/**
 * Execute a command
 * If test is run locally, you can execute any command, limit is the rights given to the user
 * If test is run through seleniumRobot grid, only commands allowed by grid will be allowed
 * @param program	program to execute
 * @param charset	the charset used to read program output
 * @param timeout	timeout in seconds. After this duration, program will be terminated
 * @param args		arguments to give to program
 */
public static String executeCommand(String program, int timeout, Charset charset, String... args) {
    if (SeleniumTestsContextManager.getThreadContext().getRunMode() == DriverMode.LOCAL) {
        String[] cmd = new String[] { program };
        cmd = ArrayUtils.addAll(cmd, args);
        return OSCommand.executeCommandAndWait(cmd, timeout, charset);
    } else if (SeleniumTestsContextManager.getThreadContext().getRunMode() == DriverMode.GRID) {
        SeleniumGridConnector gridConnector = SeleniumTestsContextManager.getThreadContext().getSeleniumGridConnector();
        if (gridConnector != null) {
            return gridConnector.executeCommand(program, timeout, args);
        } else {
            throw new ScenarioException(ERROR_NO_GRID_CONNECTOR_ACTIVE);
        }
    } else {
        throw new ScenarioException("command execution only supported in local and grid mode");
    }
}
Also used : SeleniumGridConnector(com.seleniumtests.connectors.selenium.SeleniumGridConnector) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 2 with SeleniumGridConnector

use of com.seleniumtests.connectors.selenium.SeleniumGridConnector in project seleniumRobot by bhecquet.

the class TestTestTasks method testExecuteCommandNotUsed.

/**
 * Test when the grid connector is not initialized
 * When grid connector does not return any sessionId, it's not active
 * @param testNGCtx
 */
@Test(groups = { "ut" }, expectedExceptions = ScenarioException.class)
public void testExecuteCommandNotUsed(final ITestContext testNGCtx) {
    SeleniumGridConnector gridConnector = spy(new SeleniumRobotGridConnector("http://localhost:4444/hub/wd"));
    doReturn("hello guys").when(gridConnector).executeCommand("echo", "hello");
    PowerMockito.mockStatic(SeleniumGridConnectorFactory.class);
    PowerMockito.when(SeleniumGridConnectorFactory.getInstances(Arrays.asList("http://localhost:4444/hub/wd"))).thenReturn(Arrays.asList(gridConnector));
    try {
        System.setProperty(SeleniumTestsContext.RUN_MODE, "grid");
        System.setProperty(SeleniumTestsContext.WEB_DRIVER_GRID, "http://localhost:4444/hub/wd");
        initThreadContext(testNGCtx);
        SeleniumTestsContextManager.getThreadContext().getSeleniumGridConnectors();
        TestTasks.executeCommand("echo", "hello");
    } finally {
        System.clearProperty(SeleniumTestsContext.RUN_MODE);
        System.clearProperty(SeleniumTestsContext.WEB_DRIVER_GRID);
    }
}
Also used : SeleniumRobotGridConnector(com.seleniumtests.connectors.selenium.SeleniumRobotGridConnector) SeleniumGridConnector(com.seleniumtests.connectors.selenium.SeleniumGridConnector) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) XmlTest(org.testng.xml.XmlTest) GenericTest(com.seleniumtests.GenericTest) MockitoTest(com.seleniumtests.MockitoTest)

Example 3 with SeleniumGridConnector

use of com.seleniumtests.connectors.selenium.SeleniumGridConnector in project seleniumRobot by bhecquet.

the class TestTestTasks method testKillProcessGridNotUsed.

/**
 * Test when the grid connector is not initialized
 * When grid connector does not return any sessionId, it's not active
 * @param testNGCtx
 */
@Test(groups = { "ut" }, expectedExceptions = ScenarioException.class)
public void testKillProcessGridNotUsed(final ITestContext testNGCtx) {
    SeleniumGridConnector gridConnector = spy(new SeleniumRobotGridConnector("http://localhost:4444/hub/wd"));
    doNothing().when(gridConnector).killProcess("some_process");
    PowerMockito.mockStatic(SeleniumGridConnectorFactory.class);
    PowerMockito.when(SeleniumGridConnectorFactory.getInstances(Arrays.asList("http://localhost:4444/hub/wd"))).thenReturn(Arrays.asList(gridConnector));
    try {
        System.setProperty(SeleniumTestsContext.RUN_MODE, "grid");
        System.setProperty(SeleniumTestsContext.WEB_DRIVER_GRID, "http://localhost:4444/hub/wd");
        initThreadContext(testNGCtx);
        SeleniumTestsContextManager.getThreadContext().getSeleniumGridConnectors();
        TestTasks.killProcess("some_process");
        verify(gridConnector).killProcess("some_process");
    } finally {
        System.clearProperty(SeleniumTestsContext.RUN_MODE);
        System.clearProperty(SeleniumTestsContext.WEB_DRIVER_GRID);
    }
}
Also used : SeleniumRobotGridConnector(com.seleniumtests.connectors.selenium.SeleniumRobotGridConnector) SeleniumGridConnector(com.seleniumtests.connectors.selenium.SeleniumGridConnector) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) XmlTest(org.testng.xml.XmlTest) GenericTest(com.seleniumtests.GenericTest) MockitoTest(com.seleniumtests.MockitoTest)

Example 4 with SeleniumGridConnector

use of com.seleniumtests.connectors.selenium.SeleniumGridConnector in project seleniumRobot by bhecquet.

the class TestTestTasks method testExecuteCommandGridWithTimeout.

@Test(groups = { "ut" })
public void testExecuteCommandGridWithTimeout(final ITestContext testNGCtx) {
    SeleniumGridConnector gridConnector = spy(new SeleniumRobotGridConnector("http://localhost:4444/hub/wd"));
    gridConnector.setNodeUrl("http://localhost:5555/hub/wd");
    doReturn("hello guys").when(gridConnector).executeCommand("echo", 25, "hello");
    // grid connector is in use only if session Id exists
    doReturn(new SessionId("1234")).when(gridConnector).getSessionId();
    PowerMockito.mockStatic(SeleniumGridConnectorFactory.class);
    PowerMockito.when(SeleniumGridConnectorFactory.getInstances(Arrays.asList("http://localhost:4444/hub/wd"))).thenReturn(Arrays.asList(gridConnector));
    try {
        System.setProperty(SeleniumTestsContext.RUN_MODE, "grid");
        System.setProperty(SeleniumTestsContext.WEB_DRIVER_GRID, "http://localhost:4444/hub/wd");
        initThreadContext(testNGCtx);
        SeleniumTestsContextManager.getThreadContext().getSeleniumGridConnectors();
        String response = TestTasks.executeCommand("echo", 25, null, "hello");
        Assert.assertEquals(response, "hello guys");
    } finally {
        System.clearProperty(SeleniumTestsContext.RUN_MODE);
        System.clearProperty(SeleniumTestsContext.WEB_DRIVER_GRID);
    }
}
Also used : SeleniumRobotGridConnector(com.seleniumtests.connectors.selenium.SeleniumRobotGridConnector) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SeleniumGridConnector(com.seleniumtests.connectors.selenium.SeleniumGridConnector) SessionId(org.openqa.selenium.remote.SessionId) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) XmlTest(org.testng.xml.XmlTest) GenericTest(com.seleniumtests.GenericTest) MockitoTest(com.seleniumtests.MockitoTest)

Example 5 with SeleniumGridConnector

use of com.seleniumtests.connectors.selenium.SeleniumGridConnector in project seleniumRobot by bhecquet.

the class TestTestTasks method testKillProcessOtherRunMode.

/**
 * Test kill process is not called when not using local or grid mode
 * @param testNGCtx
 */
@Test(groups = { "ut" })
public void testKillProcessOtherRunMode(final ITestContext testNGCtx) {
    SeleniumGridConnector gridConnector = spy(new SeleniumRobotGridConnector("http://saucelabs:4444/hub/wd"));
    PowerMockito.mockStatic(SeleniumGridConnectorFactory.class);
    PowerMockito.when(SeleniumGridConnectorFactory.getInstances(Arrays.asList("http://saucelabs:4444/hub/wd"))).thenReturn(Arrays.asList(gridConnector));
    try {
        System.setProperty(SeleniumTestsContext.RUN_MODE, "saucelabs");
        System.setProperty(SeleniumTestsContext.PLATFORM, "windows");
        System.setProperty(SeleniumTestsContext.WEB_DRIVER_GRID, "http://saucelabs:4444/hub/wd");
        initThreadContext(testNGCtx);
        TestTasks.killProcess("some_process");
        verify(gridConnector, never()).killProcess("some_process");
    } finally {
        System.clearProperty(SeleniumTestsContext.RUN_MODE);
        System.clearProperty(SeleniumTestsContext.PLATFORM);
        System.clearProperty(SeleniumTestsContext.WEB_DRIVER_GRID);
    }
}
Also used : SeleniumRobotGridConnector(com.seleniumtests.connectors.selenium.SeleniumRobotGridConnector) SeleniumGridConnector(com.seleniumtests.connectors.selenium.SeleniumGridConnector) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) XmlTest(org.testng.xml.XmlTest) GenericTest(com.seleniumtests.GenericTest) MockitoTest(com.seleniumtests.MockitoTest)

Aggregations

SeleniumGridConnector (com.seleniumtests.connectors.selenium.SeleniumGridConnector)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)28 Test (org.testng.annotations.Test)28 SeleniumRobotGridConnector (com.seleniumtests.connectors.selenium.SeleniumRobotGridConnector)19 ConnectorsTest (com.seleniumtests.ConnectorsTest)18 GenericTest (com.seleniumtests.GenericTest)13 XmlTest (org.testng.xml.XmlTest)13 MockitoTest (com.seleniumtests.MockitoTest)10 SessionId (org.openqa.selenium.remote.SessionId)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 ScenarioException (com.seleniumtests.customexception.ScenarioException)3 DesiredCapabilities (org.openqa.selenium.remote.DesiredCapabilities)3 ITestResult (org.testng.ITestResult)3 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)2 InputStream (java.io.InputStream)2 StringInputStream (org.apache.tools.ant.filters.StringInputStream)2 SessionNotCreatedException (org.openqa.selenium.SessionNotCreatedException)2 RemoteWebDriver (org.openqa.selenium.remote.RemoteWebDriver)2 SeleniumGridNodeNotAvailable (com.seleniumtests.customexception.SeleniumGridNodeNotAvailable)1 SeleniumRobotLogger (com.seleniumtests.util.logging.SeleniumRobotLogger)1