Automatically Generate Java Code from Recorded Tests

TestProject2BAutomation2BTutorial

Today we will explore TestProject to see how the record and play makes our life easy to do the automation testing.
Sign up to the TestProject. Its free to use and provide 2 GB space for free users.

Scenario to test with TestProject

We will automate the validation of the sign up form and verify the error with Recorded tests. We will also see how to generate the code from the recorded tests and they are similar to the selenium syntaxes.

Signup up and configuration of TestProject

1. Signup to TestProject

testprojectsignup onlyfullstack

2. Install and Register TestProject Agent

TestProject will require an agent to be installed on your system to record and run the automated tests. You can follow below steps to setup agent on your local system.
https://docs.testproject.io/getting-started/installation-and-setup

Screenshot2B2020 02 042Bat2B16.16.24

After installation of your agent you will need to register it to the TestProject account.
Before registering, make sure the agent is installed and running. You should see the TestProject icon in the system tray.

Screenshot2B2020 02 042Bat2B16.23.05

So now we are ready to record our automation test cases.

Automation with TestProject

The TestProject dashboard will show the projects. Create your new project to start with.

Screenshot2B2020 02 042Bat2B16.23.48

Lets jump into the newly created project.

Screenshot2B2020 02 042Bat2B16.24.32

Here Test are the automated test cases for different scenarios and you can add them in different Jobs which you can run manually or schedule to run at particular time.

1. Create a new Test Case

On the TestProject dashboard you will have a Test Case panel to Add a new Test.
Give a meaningful name to that test case as the TestProject will generate a class name with test case name when you generate the code of that recorded automation.

Let’s automate a validation scenario of the sign-up page and see how easy it is to use TestProject record and play tool.

A. Create a new test case named Signup empty fields validation
B. Give the application url of sign up page and click on Start Record.
C. We will do our sign up testing on below url : https://nextstep.tcs.com/campus/#/registrationPage

Screenshot2B2020 02 042Bat2B16.25.45

D. Click on Submit without providing any information into the text fields.
E. All the error messages will come up, now to assert an error message – Click on the error message html element and go to option and the select the desired assertion or validation you want to perform. Here I want to check if the error message is same what I expect then I will go to Validation -> Contains text?. See how easy it is to do the validations.

Screenshot2B2020 02 042Bat2B16.27.40

TestProject provides variety of the Actions and Attributes to play around and do some testing.

Screenshot2B2020 02 042Bat2B16.28.24

You can also get the attribute details like xpath, class and text of that attribute.

Screenshot2B2020 02 042Bat2B16.29.05

These are the steps generated after our test case record. Here each step is very elaborative and easy to understand, and you can also modify each step as and when you need.

Screenshot2B2020 02 042Bat2B16.30.37

Wohhhh, that was so easy. I did this in just 10 minutes without looking at any code. Let’s generate the code from this test case and lets try to modify the code and add new test case via code.

2. Code Generation from TestProject

Screenshot2B2020 02 042Bat2B16.31.38

You can generate the source code into Java and C#. You can also specify the package structure for it.

Screenshot2B2020 02 042Bat2B16.32.28

Code review of TestProjects generate code

Let’s dive into the code.
TestProject will create test cases in separate class and all the steps will also come as comments on the java code so you can easily identify and understand the code.
One of the best things of TestProject is that it has the same methods which selenium has so it’s same and you don’t have learn extra to use TestProject if you are an Automation Test Engineer.

public ExecutionResult execute(WebTestHelper helper) throws FailureException {
  WebDriver driver = helper.getDriver();
  // set timeout for driver actions (similar to step timeout)
  driver.setTimeout(15000);
  TestReporter report = helper.getReporter();
  By by;
  boolean booleanResult;

  // 1. Navigate to '{{ApplicationURL}}'
  //    Navigates the specified URL (Auto-generated)
  booleanResult = driver.testproject().navigateToUrl(ApplicationURL);
  report.step(String.format("Navigate to '%s'",ApplicationURL), booleanResult, TakeScreenshotConditionType.Failure);

  // 2. Click 'Register Now'
  by = By.xpath("//*[@id='mainDivision']/div/div/section[1]/div[2]/div[3]/button");
  booleanResult = driver.testproject().click(by);
  report.step("Click 'Register Now'", booleanResult, TakeScreenshotConditionType.Failure);

  // 3. Click 'DIV'
  by = By.xpath("//*[@id='btnSlctBPS']");
  booleanResult = driver.testproject().click(by);
  report.step("Click 'DIV'", booleanResult, TakeScreenshotConditionType.Failure);

  // 4. Click 'btnSlctBPS'
  by = By.id("btnSlctBPS");
  booleanResult = driver.testproject().click(by);
  report.step("Click 'btnSlctBPS'", booleanResult, TakeScreenshotConditionType.Failure);

  // 5. Click 'overBody'
  by = By.id("overBody");
  booleanResult = driver.testproject().click(by);
  report.step("Click 'overBody'", booleanResult, TakeScreenshotConditionType.Failure);

  // 6. Scroll window by ('0','1349')
  booleanResult = driver.testproject().scrollWindow(0,1349);
  report.step("Scroll window by ('0','1349')", booleanResult, TakeScreenshotConditionType.Failure);

  // 7. Click 'btnRegSubmit'
  by = By.id("btnRegSubmit");
  booleanResult = driver.testproject().click(by);
  report.step("Click 'btnRegSubmit'", booleanResult, TakeScreenshotConditionType.Failure);

  // 8. Scroll window by ('0','-100')
  booleanResult = driver.testproject().scrollWindow(0,-100);
  report.step("Scroll window by ('0','-100')", booleanResult, TakeScreenshotConditionType.Failure);

  // 9. 'You can't leave this empty.' contains text 'empty'?
  by = By.xpath("//div[4]/span[. = concat('You can', "'", 't leave this empty.')]");
  booleanResult = driver.testproject().containsText(by,"empty");
  report.step("'You can't leave this empty.' contains text 'empty'?", booleanResult, TakeScreenshotConditionType.Failure);

  return ExecutionResult.PASSED;
}

Lets add our new test case where we want to check the text of error message of first name if we don’t give any value and submit the form. The code will be same as the Title and all we need to do is change the XPath of the field and its done.

// 9. 'You can't leave this empty.' contains text 'empty'?
  by = By.xpath("//div[5]/span[. = concat('You can', "'", 't leave this empty.')]");
  booleanResult = driver.testproject().containsText(by,"empty");
  report.step("'You can't leave this empty.' contains text 'empty'?", booleanResult, TakeScreenshotConditionType.Failure);

We have automated the validation of the sign up form with record and play technique and found out all the features of TestProject.