Test Project – Coded Test Case

Similar to add ons, we can also create coded test cases which can be used a stand alone test case or can be a test case which can used by other test cases e.g. a simple log in test case.

In this section, lets see how to create coded test case. So what are the ingredients (coding is like a recipe anyway 😀)

For the coded test cases, we will use

  • IntelliJ as IDE
  • https://github.com/testproject-io/java-sdk-examples as reference
  • descriptor.xml file
  • Page Object Model design pattern
  • Test Project Java SDK
  • Good understanding of Maven
  • Good understanding of Selenium automation
  • DEV_TOKEN
  • Hardcoded value is used

Test case scenario

Log in test case which fills up Full Name, Password and click on Log in button.

Create Project in Intellij

Create a maven project and make sure the structure is similar to the screenshot attached.

Description of the project structure

TestCases package
Our test cases will be created in this package

Runners Package
Holds PageObjects for the test cases
Test case runner to make sure that the test case runs locally fine

descriptor.xml file
Navigate to https://github.com/testproject-io/java-sdk-examples/tree/master/Web/Addon/src/main and copy the descriptor.xml file and add to the project in main folder.

Update pom.xml file

Its exactly similar to the way we have added in the Addon blog – Click here

Content of pom.xml file

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelversion>4.0.0</modelversion>
 
    <groupid>groupId</groupid>
    <artifactid>TestProjectTestCases</artifactid>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <tp .sdk.path="">${project.basedir}/../../io.testproject.sdk.java.jar</tp>
        <project .build.sourceencoding="">UTF-8</project>
    </properties>
 
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupid>org.junit.jupiter</groupid>
            <artifactid>junit-jupiter-api</artifactid>
            <version>5.3.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-runner -->
        <dependency>
            <groupid>org.junit.platform</groupid>
            <artifactid>junit-platform-runner</artifactid>
            <version>1.3.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
        <dependency>
            <groupid>org.junit.jupiter</groupid>
            <artifactid>junit-jupiter-engine</artifactid>
            <version>5.3.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
        <dependency>
            <groupid>org.junit.vintage</groupid>
            <artifactid>junit-vintage-engine</artifactid>
            <version>5.3.1</version>
            <scope>test</scope>
        </dependency>
        <!-- TestProject SDK -->
        <dependency>
            <groupid>io.testproject</groupid>
            <artifactid>java-sdk</artifactid>
            <version>1.0</version>
            <!-- Update the location of the sdk as required -->
            <systempath>C:/testproject/TestProject_SDK_0.61.0.jar</systempath>
            <scope>system</scope> <!-- Local file won't be assembled by maven-assembly-plugin -->
        </dependency>
    </dependencies>
 
    <build>
        <sourcedirectory>src</sourcedirectory>
        <plugins>
            <!-- Assembly Plugin - Create a JAR with dependencies for uploading to TestProject -->
            <plugin>
                <artifactid>maven-assembly-plugin</artifactid>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/descriptor.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--Tests Plugin-->
            <plugin>
                <artifactid>maven-surefire-plugin</artifactid>
                <version>2.22.0</version>
                <configuration>
                    <skiptests>true</skiptests>
                </configuration>
            </plugin>
            <!-- Compile Plugin -->
            <plugin>
                <artifactid>maven-compiler-plugin</artifactid>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
     
</project>

Create the page object class and name it as MyFirstTestCasePage.java

No need to explain this file. It’s straight forward page object model pattern

package main.Runners;
 
import io.testproject.java.sdk.v2.drivers.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
 
/**
 * Created by Sisirkant on 5/2/2020.
 */
public class MyFirstTestCasePage {
 
    public MyFirstTestCasePage(WebDriver driver){
        PageFactory.initElements(driver, this);
    }
 
    @FindBy (xpath = "//input[@id='name']")
    private WebElement txtFullName;
 
    @FindBy(xpath = "//input[@id='password']")
    private WebElement txtPassword;
 
    @FindBy(xpath = " //button[@id='login']")
    private WebElement btnLogin;
 
    public void enterFullName(){
        txtFullName.sendKeys("Sisirkant Prusty");
    }
 
    public void enterPassword(){
        txtPassword.sendKeys("12345");
    }
 
    public void clickLogin() throws InterruptedException {
        btnLogin.click();
        Thread.sleep(2000);
    }
}

Lets create our first test case – MyFirstTestCasePage.java

package main.TestCases;
 
import io.testproject.java.annotations.v2.Test;
import io.testproject.java.sdk.v2.drivers.WebDriver;
import io.testproject.java.sdk.v2.enums.ExecutionResult;
import io.testproject.java.sdk.v2.exceptions.FailureException;
import io.testproject.java.sdk.v2.tests.WebTest;
import io.testproject.java.sdk.v2.tests.helpers.WebTestHelper;
import main.Runners.MyFirstTestCasePage;
 
import static java.lang.Thread.sleep;
 
 
/**
 * Created by Sisirkant on 5/2/2020.
 */
 
@Test(name = "My First Login Test case")
public class MyFirstTestCase implements WebTest {
 
    @Override
    public ExecutionResult execute(WebTestHelper webTestHelper) throws FailureException {
        WebDriver driver = webTestHelper.getDriver();
        MyFirstTestCasePage myFirstTestCasePage = new MyFirstTestCasePage(driver);
        driver.get("https://example.testproject.io/web/");
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        myFirstTestCasePage.enterFullName();
        myFirstTestCasePage.enterPassword();
        try {
            myFirstTestCasePage.clickLogin();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
 
        return ExecutionResult.PASSED;
    }
}

Check the line no 18 of the code. That name will appear as the test case name in testproject once uploaded.

Time to create a runner file to test our test case locally – MyFirstTestCaseRunner.java

package main.Runners;
 
import io.testproject.java.classes.DriverSettings;
import io.testproject.java.enums.DriverType;
import io.testproject.java.sdk.v2.Runner;
import main.TestCases.MyFirstTestCase;
 
/**
 * Created by Sisirkant on 5/2/2020.
 */
public class MyFirstTestCaseRunner {
 
    private static  final String DEV_TOKEN ="P5nXPwPbFYTvzPGKFDsttlu0qfMZuD5D8SnCs--YhAU";
    public static void main(String[] args) throws  Exception{
        DriverSettings driverSettings = new DriverSettings(DriverType.Chrome);
        try (Runner runner = new Runner(DEV_TOKEN, driverSettings)){
 
            MyFirstTestCase myFirstTestCase = new MyFirstTestCase();
            runner.run(myFirstTestCase);
        }
    }
}

Run the MyFirstTestCaseRunner.java file to make sure the test case that we have documented is running fine.
It should run fine.

Create package and upload it to TestProject

Go to Terminal of IntelliJ and run the following command

mvn clean validate compile test package

Once the above command is run, we should see the necessary package under target folder.

Once the above command is run, we should see the necessary package under target folder.

Upload the package to TestProject

In TestProject,

  • Create a new test case
  • Select Code
  • Click on Next
  • In Upload your code overlay, upload the file
  • Click on next
  • In the Create a test package overlay check the details and click on Next
  • Enter a Test Package Name, Description and select application if you want (not needed here as we have hard coded url) and click Next
  • Click Start Testing

Once the test case is uploaded, we can see the test case package and test case name

Time to run the test case (exciting!!)
Run the test case. Voila! Its working

Note that everything is hard code in the test case. The intention was to see  how to create the coded test case. In the next blog we will see how to parameterize the test case so that we can change the parameter during run time in TestProject. 

About This Site

The main aim of this site is to share knowledge with fellow software test specialists who are keen to grow both technically and professionally.

Categories