How to mock a response in Selenium

 First, let us understand why there is a need to mock? 

UI automation is an expensive thing to do as it's flaky and time-consuming not only in development but as well as in execution.

Use Case 1

Imagine a situation, where we have an e-Commerce application where there is a new rating and review functionality.

Now, in order to test this functionality, I need a lot of variations of test data from 0 reviews and ratings to probably a higher number.

If I have to automate this scenario, that means, I need to

  1.  create multiple users
  2. Add their basic details such as Address, payments, etc
  3. Add that particular product in all the multiple users who will give ratings and reviews
  4. And finally, add ratings and reviews
To achieve the above, we have 2 options

1. Using API's  directly and follow the above steps

2. Mock the response of the API which is getting these ratings and reviews details.

If you are using Cypress, it is very much possible. todo using their intercept command.

For Selenium users, unfortunately for 3.x version does not provide any mocking capability.

However, you can use external this lib for mocking.

Use case 2 with code and video recording

Create a user and login to https://react-redux.realworld.io/

Now, our task is to mock the Tags API. Let us say, I want to display just 2 tags  --> Tag1, Tag2

Tags before mocking:



Tags after mocking:




Code:

public class MockTags {

WebDriver driver;
WowXHR wowXhr;
@BeforeSuite
public void beforeSuite() throws DriverNotSupportedException {
WebDriverManager.chromedriver().setup();
wowXhr = new WowXHR(new ChromeDriver());
driver = wowXhr.getMockDriver();
driver.navigate().to("https://react-redux.realworld.io/");
wowXhr.mock().add(
Mockable.whenGET("/api/tags")
.respond(
Mockable.mockResponse()
.withBody("{\"tags\" : [\"mockTag1\", \"mockTag2\"]}")
)
);
}

@Test
public void mockTest() throws InterruptedException {

driver.findElement(By.linkText("Sign in")).click();
driver.findElement(By.cssSelector("input[type='email']")).sendKeys("abc@tester.com");
driver.findElement(By.cssSelector("input[type='password']")).sendKeys("Qwerty@123");
driver.findElement(By.xpath("//button[@type='submit']")).click();

Thread.sleep(5000);



}

}


Dependencies used:-

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>

<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.4.1</version>
</dependency>

<dependency>
<groupId>io.github.sudharsan-selvaraj</groupId>
<artifactId>wow-xhr</artifactId>
<version>1.0.0</version>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
<exclusions>
<exclusion>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
</exclusion>
</exclusions>
</dependency>


GIF for the entire execution:
 


Comments

Post a Comment

Popular posts from this blog

Some useful links for testing

Generic Checklist in case you want to consider Cypress