Eclipse Project Web

Basic knowledge of Java programming language and Eclipse is required.
It is assumed that you are using the default installation of MyITest4U.

MyITest4U comes with an Eclipse project. This can be used to fine tune MyITest4U. The Eclipse project has to be imported into Eclipse before it can be used. The workspace should be: C:\AutoTest\MyITest4U\TestEngine\Java. The project is in the folder C:\AutoTest\MyITest4U\TestEngine\Java\Api.

Logging

The following method can be used for logging:

       
     /**
     * Logging example. The log statements can be used anywhere in a method. The log
     * will be written to ../MyITest4U/TestLog
     */
     public void loggingExample() {
        Exception e = new Exception();
        Log.LOGGER.log(MyLevel.TESTLOG, "Will appear in the test log."); //$NON-NLS-1$
        Log.LOGGER.log(MyLevel.TESTLOG, "Will appear in the test log showing the exception.", e); //$NON-NLS-1$

        Log.LOGGER.log(Level.INFO, "Will appear in the log."); //$NON-NLS-1$
        Log.LOGGER.log(Level.INFO, "Will appear in the log showing the exception.", e); //$NON-NLS-1$
    }
    
      

Creating Your Own Actions

MyITest4U allows you to create your own actions.
Create your own action by copying and changing the TemplateAction (com.myitest4u.selenium.api.actions).

Enter your action code in the method doAction.

Enter a rule in the method addAction. The rule determines for which GUI elements your custom action is shown in the editor. Return true to always show your custom action.

The method showException determines if the stack trace is shown in case of an error or not. A return value of true will show the stack trace. A return value of false will hide the stack trace.

Open the class MyActionList.
Add your action to the action list.

Compile the project.
Your custom action is now ready to use.

       
public class TemplateAction extends Rules implements IAction {

    /**
     * @param name        The name of the action. It has to be unique (no test is
     *                    done for uniqueness).
     * @param description Description of the action.
     * @param parameter   Description of the parameters used by the action.
     */
    public TemplateAction(String name, String description, String parameter, boolean i18n) {
        super(name, description, parameter, i18n);
    }

    /**
     * Contains the code for the action to be done.
     * 
     * @param element    The web element used for this action. null for all
     *                   NoGuiElement actions.
     * @param driver
     * @param kindOfRun  Used if a comparison between reference and run is done.
     * @param testStep
     * @param errorStart Used errorStart if you want to log an error directly in
     *                   this method.
     * @return true if no error happens.
     *         false if an error happens.
     * @throws Exception
     */
    @Override
    public boolean doAction(WebElement element, WebDriver driver, String kindOfRun, WebTestStep testStep,
            String errorStart) throws Exception {
        
        // TODO enter the code for the action to be done
        // return true if no error happens

        // Use this logging to show an error without a screen shot
        // return false
        Log.LOGGER.log(MyLevel.TESTLOG, errorStart + " test error"); //$NON-NLS-1$

        // Use this to show an error and screen shot
        String toCheck = "hans"; //$NON-NLS-1$
        String value = "peter"; //$NON-NLS-1$
        throw new Exception("Value was \"" + toCheck + "\". It should be: " + value); //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * Used to define rules for which elements this action is shown.
     * 
     * @param testStep
     * @return true if the action should be shown.
     *         false if the action should not be shown.
     */
    @Override
    public boolean addAction(WebTestStep testStep) {
        
        // TODO Add rule for which kind of GUI elements this action can be used
        // To use the action everywhere return true with no rule at all

        /**
         * The following rules exist:
         * - isGuiElement(WebTestStep testStep)
         * - isSpecialKey(WebTestStep testStep)
         * - isModifierKey(WebTestStep testStep)
         * - isKey(WebTestStep testStep)
         * - isTextElement(WebTestStep testStep)
         * - isSelectElement(WebTestStep testStep)
         * - isCheckSelectElement(WebTestStep testStep)
         * - isNoGuiElement(testStep)
         */
        return true;
    }

    /** 
     * @return true if the stack trace of the exception should be shown.
     *         false will only show the exception message.
     */
    @Override
    public boolean showException() {
        return false;
    }

     /**
     * @return true if the action is done on a GUI element.
     *         false if the action does not need a GUI element.
     */
    @Override
    public boolean needsGuiElement() {
        return true;
    }
}
    
      

Handling Of Language Dependent Strings

The class MyLanguageHandler can be used to handle language dependent strings. Therefore you need to implement the method setToBeHandled(String toBeHandled). A very basic example of an implementation of setToBeHandled is:

       
     /**
     * Implement this method to set the language dependent strings.
     * The method needs to call setString(String theTranslatedString).
     *
     * @param toBeHandled: The input to get the language dependent string. This
     *        could be the resource bundle key or an id for the language dependent
     *        string on the database.
     */

    public void setToBeHandled(String toBeHandled) {

        if (getAppLanguage() != null && getAppLanguage().equals("de")) {
            setLocale(new Locale("de"));
        }

        String value = ""; //$NON-NLS-1$
        if (toBeHandled != null) {
            value = toBeHandled;
        }

        setString(MyMessages.getString(value));

        setString(toBeHandled);
    }
    
      

The language change will happen at the following places:

  • The checkpoints Find and NotToFind in the parameter files.
  • The Global Find and Global NotToFind.
  • The text if a GUI element is found using ByLinkText or ByXPathText.
  • All actions using a language dependent text e.g. CheckAttribute.

Web Drivers

Selenium Web Driver allows you to set specific options for your Web Driver.
MyITest4U allows you to set the web driver options using the class WebDriverOptions.
The driver options can be set by implementing a method which sets the driver options e.g. Object getChromeOptions().
The method used to set the web driver options has to return an Object. The Object has to be the class implementing the Selenium Web Driver options e.g. ChromeOptions (Instantiation of the Selenium Web Driver is done by reflection).
This method is used in the method getDriverOpitons(String driverName, String option).
The method getDriverOpitons(String driverName, String option) is called when the Web Driver is instantiated. The returned DriverOptionsContainer object contains the two fields String optionsClassName and Object options. They need to be set accordingly.
An example of the class WebDriverOptions is given below.

       
     /**
      * Use this class to set your own web driver options.
      * 
      * @author maurusspescha
      *
      */
     public class WebDriverOptions {
     
         private Object getChromeOptions() {
             ChromeOptions options = new ChromeOptions();
     
             options.addArguments("start-maximized");
             // stores the browser data like cookies in the given directory
             // options.addArguments("--user-data-dir=C:\\temp\\auto");
             // options.addArguments("--disable-extensions");
     
             // removes the message "Chrome is being controlled by automated test software"
             // is not supported any more 25.02.2020
             // options.addArguments("--disable-infobars");
     
             return options;
         }
     
         private Object getFirefoxOptions() {
     
             FirefoxOptions options = new FirefoxOptions();
     
             options.addArguments("start-maximized");
     
             return options;
     
         }
     
         private Object getEdgeOptions() {
     
             EdgeOptions options = new EdgeOptions();
     
             // TODO add your options here
             return options;
     
         }
     
         private Object getInternetExplorerOptions() {
     
             InternetExplorerOptions options = new InternetExplorerOptions();
     
             // TODO add your options here
     
             return options;
     
         }
     
         /**
          * This method is called by the driver instantiation method.
          * 
          * @param driverName
          * @param option     can be use to divide the options further.
          * @return DriverOptionsContainer The DriverOptionsContainer class contains two
          *         methods:
          *         - setOptions(Object options) 
          *         - setOptionsClassName(String optionsClassName) the class name of the driver options.
          *         
          *         The parameters of these two methods need to be set.
          *         A return value of null can be used to set no Web Driver Options at all.
          */
         public DriverOptionsContainer getDriverOpitons(String driverName, String option) {
             switch (driverName) {
             case "ChromeDriver":
                 DriverOptionsContainer chromeDOC = new DriverOptionsContainer();
                 chromeDOC.setOptions(getChromeOptions());
                 chromeDOC.setOptionsClassName("org.openqa.selenium.chrome.ChromeOptions");
                 return chromeDOC;
             case "FirefoxDriver":
                 DriverOptionsContainer firefoxDOC = new DriverOptionsContainer();
                 firefoxDOC.setOptions(getFirefoxOptions());
                 firefoxDOC.setOptionsClassName("org.openqa.selenium.firefox.FirefoxOptions");
                 return firefoxDOC;
             case "EdgeDriver":
                 DriverOptionsContainer edgeDOC = new DriverOptionsContainer();
                 edgeDOC.setOptions(getEdgeOptions());
                 edgeDOC.setOptionsClassName("org.openqa.selenium.edge.EdgeOptions");
                 return edgeDOC;
             case "IeDriver":
                 DriverOptionsContainer ieDOC = new DriverOptionsContainer();
                 ieDOC.setOptions(getInternetExplorerOptions());
                 ieDOC.setOptionsClassName("org.openqa.selenium.ie.InternetExplorerOptions");
                 return ieDOC;
             }
             return null;
         }
     }