Search in sources :

Example 31 with TestVariable

use of com.seleniumtests.core.TestVariable in project seleniumRobot by bhecquet.

the class TestSeleniumTestContext3 method testVariablesAreGetMultipleTimesWithContextCopy.

/**
 * Check that when copying context, it's possible to allow it to retrieve
 * variables from server
 *
 * @param testNGCtx
 * @param xmlTest
 * @throws Exception
 */
@Test(groups = { "ut" })
public void testVariablesAreGetMultipleTimesWithContextCopy(final ITestContext testNGCtx, final XmlTest xmlTest) throws Exception {
    Map<String, TestVariable> variables = new HashMap<>();
    variables.put("key", new TestVariable("key", "val1"));
    try {
        System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_ACTIVE, "true");
        System.setProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_URL, "http://localhost:1234");
        PowerMockito.whenNew(SeleniumRobotVariableServerConnector.class).withArguments(eq(true), eq("http://localhost:1234"), anyString(), eq(null)).thenReturn(variableServer);
        when(variableServer.isAlive()).thenReturn(true);
        when(variableServer.getVariables(0)).thenReturn(variables);
        ITestResult testResult = GenericTest.generateResult(testNGCtx, getClass());
        initThreadContext(testNGCtx, "myTest", testResult);
        SeleniumTestsContext seleniumTestsCtx = SeleniumTestsContextManager.getThreadContext();
        // do a parameter retrieving for copied context. Context is copied with
        // permitting calls to variable server
        SeleniumTestsContext seleniumTestsCtx2 = new SeleniumTestsContext(seleniumTestsCtx);
        seleniumTestsCtx2.configureContext(testResult);
        // check that variables are kept even if variable server has not been re-called
        Assert.assertEquals(seleniumTestsCtx2.getConfiguration().get("key").getValue(), "val1");
        // 2 calls, one for each context because we allow variable retrieving when
        // copying
        verify(variableServer, times(2)).getVariables(0);
    } finally {
        System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_ACTIVE);
        System.clearProperty(SeleniumTestsContext.SELENIUMROBOTSERVER_URL);
    }
}
Also used : SeleniumTestsContext(com.seleniumtests.core.SeleniumTestsContext) ITestResult(org.testng.ITestResult) HashMap(java.util.HashMap) TestVariable(com.seleniumtests.core.TestVariable) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.testng.annotations.Test) ConnectorsTest(com.seleniumtests.ConnectorsTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) XmlTest(org.testng.xml.XmlTest) GenericTest(com.seleniumtests.GenericTest)

Example 32 with TestVariable

use of com.seleniumtests.core.TestVariable in project seleniumRobot by bhecquet.

the class TestSeleniumTestContext method testBugtrackerUrlFromVariable.

@Test(groups = "ut context")
public void testBugtrackerUrlFromVariable(final ITestContext testNGCtx, final XmlTest xmlTest) {
    initThreadContext(testNGCtx);
    SeleniumTestsContextManager.getThreadContext().getConfiguration().put("bugtrackerUrl", new TestVariable("bugtrackerUrl", "http://foo.bar2"));
    Assert.assertEquals(SeleniumTestsContextManager.getThreadContext().getBugtrackerUrl(), "http://foo.bar2");
}
Also used : TestVariable(com.seleniumtests.core.TestVariable) Test(org.testng.annotations.Test) XmlTest(org.testng.xml.XmlTest) GenericTest(com.seleniumtests.GenericTest)

Example 33 with TestVariable

use of com.seleniumtests.core.TestVariable in project seleniumRobot by bhecquet.

the class TestSeleniumTestContext method testAttributeFoundInVariableAndRequested.

@Test(groups = "ut context")
public void testAttributeFoundInVariableAndRequested(final ITestContext testNGCtx, final XmlTest xmlTest) {
    initThreadContext(testNGCtx);
    SeleniumTestsContextManager.getThreadContext().getConfiguration().put("foo2", new TestVariable("foo2", "bar2"));
    SeleniumTestsContextManager.getThreadContext().setAttribute("foo", "bar");
    Assert.assertEquals(SeleniumTestsContextManager.getThreadContext().getAttribute("foo2", true), "bar2");
}
Also used : TestVariable(com.seleniumtests.core.TestVariable) Test(org.testng.annotations.Test) XmlTest(org.testng.xml.XmlTest) GenericTest(com.seleniumtests.GenericTest)

Example 34 with TestVariable

use of com.seleniumtests.core.TestVariable in project seleniumRobot by bhecquet.

the class ConfigReader method readConfig.

public Map<String, TestVariable> readConfig(InputStream iniFileStream, String environment) {
    Map<String, TestVariable> testConfig = new HashMap<>();
    try {
        Ini ini = new Ini();
        Config conf = ini.getConfig();
        conf.setGlobalSection(true);
        conf.setGlobalSectionName(GLOBAL_SECTION_NAME);
        conf.setFileEncoding(StandardCharsets.UTF_8);
        ini.setConfig(conf);
        ini.load(iniFileStream);
        Set<Entry<String, Section>> sections = ini.entrySet();
        // first get global options
        testConfig = getGlobalOptions(testConfig, sections);
        // then overwrite global options with local ones
        testConfig = getLocalOptions(testConfig, sections, environment);
        return testConfig;
    } catch (InvalidFileFormatException e) {
        throw new ConfigurationException("Invalid ini/properties file: " + iniFileStream + ", check there is no BOM for encoding");
    } catch (IOException e) {
        throw new ConfigurationException(String.format("File does not exist %s: %s", iniFileStream, e.getMessage()));
    }
}
Also used : InvalidFileFormatException(org.ini4j.InvalidFileFormatException) Entry(java.util.Map.Entry) HashMap(java.util.HashMap) Ini(org.ini4j.Ini) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) Config(org.ini4j.Config) TestVariable(com.seleniumtests.core.TestVariable) IOException(java.io.IOException)

Example 35 with TestVariable

use of com.seleniumtests.core.TestVariable in project seleniumRobot by bhecquet.

the class ConfigReader method readConfig.

/**
 * read configuration from default config file (env.ini or config.ini)
 * read any other provided configuration files (through loadIni parameter)
 * @return
 */
public Map<String, TestVariable> readConfig() {
    Map<String, TestVariable> variables = new HashMap<>();
    try (InputStream iniFileStream = FileUtils.openInputStream(getConfigFile())) {
        variables.putAll(readConfig(iniFileStream, testEnv));
    } catch (NullPointerException e) {
        logger.warn("config file is null, check config path has been set using 'SeleniumTestsContextManager.generateApplicationPath()'");
        return variables;
    } catch (IOException e1) {
        logger.warn("no valid config.ini file for this application");
        return variables;
    }
    // read additional files
    if (iniFiles != null) {
        for (String fileName : iniFiles.split(",")) {
            fileName = fileName.trim();
            File currentConfFile = Paths.get(SeleniumTestsContextManager.getConfigPath(), fileName).toFile();
            logger.info("reading file " + currentConfFile.getAbsolutePath());
            try (InputStream iniFileStream = FileUtils.openInputStream(currentConfFile)) {
                variables.putAll(readConfig(iniFileStream, testEnv));
            } catch (IOException e) {
                throw new ConfigurationException(String.format("File %s does not exist in data/<app>/config folder", fileName));
            }
        }
    }
    return variables;
}
Also used : HashMap(java.util.HashMap) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) InputStream(java.io.InputStream) TestVariable(com.seleniumtests.core.TestVariable) IOException(java.io.IOException) File(java.io.File)

Aggregations

TestVariable (com.seleniumtests.core.TestVariable)72 Test (org.testng.annotations.Test)66 GenericTest (com.seleniumtests.GenericTest)53 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)38 XmlTest (org.testng.xml.XmlTest)34 ConnectorsTest (com.seleniumtests.ConnectorsTest)19 MockitoTest (com.seleniumtests.MockitoTest)19 ITestResult (org.testng.ITestResult)14 HashMap (java.util.HashMap)13 SeleniumRobotVariableServerConnector (com.seleniumtests.connectors.selenium.SeleniumRobotVariableServerConnector)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)10 SeleniumTestsContext (com.seleniumtests.core.SeleniumTestsContext)7 File (java.io.File)3 ArrayList (java.util.ArrayList)3 JSONObject (kong.unirest.json.JSONObject)3 BrowserExtension (com.seleniumtests.browserfactory.BrowserExtension)2 ConfigReader (com.seleniumtests.core.config.ConfigReader)2 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)2 IOException (java.io.IOException)2 HttpRequest (kong.unirest.HttpRequest)2