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 Addob blog - https://selenium-auto.blogspot.com/p/testprojectaddon.html

Content of pom.xml file


    4.0.0

    groupId
    TestProjectTestCases
    1.0-SNAPSHOT
    
        ${project.basedir}/../../io.testproject.sdk.java.jar
        UTF-8
    

    
        
        
            org.junit.jupiter
            junit-jupiter-api
            5.3.1
        
        
        
            org.junit.platform
            junit-platform-runner
            1.3.1
        
        
        
            org.junit.jupiter
            junit-jupiter-engine
            5.3.1
            test
        
        
        
            org.junit.vintage
            junit-vintage-engine
            5.3.1
            test
        
        
        
            io.testproject
            java-sdk
            1.0
            
            C:/testproject/TestProject_SDK_0.61.0.jar
            system 
        
    

    
        src
        
            
            
                maven-assembly-plugin
                
                    
                        src/main/descriptor.xml
                    
                
                
                    
                        make-assembly
                        package
                        
                            single
                        
                    
                
            
            
            
                maven-surefire-plugin
                2.22.0
                
                    true
                
            
            
            
                maven-compiler-plugin
                3.5.1
                
                    1.8
                    1.8
                
            
        
    
    





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.


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. 

No comments:

Post a Comment