How to automate an online test untill result page arrives?

public class Flow1 {
AppiumDriver driver;
OkHttpClient okHttpClient=new OkHttpClient();
Request request;
By mainId = By.name(“question js-question assessment”);
public String question_id;

@BeforeTest
public void setup() throws MalformedURLException, InterruptedException
{
	DesiredCapabilities capabilities = new DesiredCapabilities();
	
	capabilities.setCapability("deviceName", "XT1624");

	  // Set BROWSER_NAME desired capability. It's Android in our case here.
	  capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");

	  // Set android VERSION desired capability. Set your mobile device's OS version.
	  capabilities.setCapability(CapabilityType.VERSION, "7.0");

	  // Set android platformName desired capability. It's Android in our case here.
	  capabilities.setCapability("platformName", "Android");

	  
	  // Set your application's appPackage if you are using any other app.
	  capabilities.setCapability("appPackage", "haygot.togyah.app.debug");
	  

	  // Set your application's appPackage if you are using any other app.
	  capabilities.setCapability("appActivity", "haygot.togyah.app.login.activities.SplashActivity");
	  capabilities.setCapability("autoGrantPermissions", true);
	 
	  capabilities.setCapability("noReset",true);
	  capabilities.setCapability("fullReset",false);
	  
	  URL url = new URL("http://127.0.1.1:4723/wd/hub");
	  driver = new AndroidDriver(url ,capabilities);
	  Thread.sleep(10000);
	  print();
	  System.out.println("number = 1");
	  
}
@Test(priority=1)
public void print(){
	System.out.println("test case");
	
}
@Test(priority=2)
public void easy2() throws InterruptedException
{
	driver.findElement(By.xpath("//*[@resource-id='haygot.togyah.app.debug:id/subject_name' and @text='Physics']")).click();
	Thread.sleep(5000);
	
	driver.findElement(By.xpath("//*[@resource-id='haygot.togyah.app.debug:id/chapter_name' and @text='Force and Pressure']")).click();
	Thread.sleep(5000);
	
	driver.findElement(By.id("haygot.togyah.app.debug:id/btn_resume_card")).click();
	Thread.sleep(5000);
	print();
	System.out.println("number = 3");
}

@Test(priority=3)
public void easy3() throws InterruptedException, JSONException
{
    //Get all the contexts in the application example: DOM, WEBVIEW , NATIVE
    //Store all the context handles in a set and print them
    Set<String> contextNames = driver.getContextHandles();
    for (String contextName : contextNames) {
	    System.out.println(contextName);
	}
	print();
	System.out.println("number = 4");
	
	//We know that the Web-view is stored in the second index i.e. 0,1
	//Moving to WEBVIEW
	driver.context((String) contextNames.toArray()[1]);
	Thread.sleep(2000);

// driver.findElement(By.xpath("//div[@class=‘label’ and text()=‘A’]")).click();
System.out.println(“number = 6”);

	//Find Question-id which is stored as an attribute for the question box 
	WebElement xx2 = driver.findElement(By.cssSelector("div[class='question js-question assessment ']"));
	String question_id = xx2.getAttribute("data-qid");
	
	//Print Question-id for further use
	System.out.println("Question id "+question_id);
	Thread.sleep(7000);

// //Call a method apiData() from the class GetData2 to fetch the correct answer
// GetData2 data = new GetData2();
// data.apiData();

	OkHttpClient okHttpClient=new OkHttpClient();
	Request request;
	//Set the question id as a variable in the URL
	String url="https://www.toppr.com/api/v5.0/permalink/r/question/"+question_id+"/";
	//String url="https://www.toppr.com/api/v5.0/permalink/r/question/123126/";
	//123126
	String correctAnswer = "";
	request=new Request.Builder().url(url).build();
	try
	{
		Response response=okHttpClient.newCall(request).execute();
		String responseString = response.body().string();
		JSONObject Jobject = new JSONObject(responseString);
		System.out.println(responseString);
		JSONArray array = Jobject.getJSONObject("data").getJSONArray("questions");
		for(int i=0;i<array.length();i++){
			JSONObject ob = array.getJSONObject(i);
			JSONArray choices = ob.getJSONArray("choices");
			for(int j=0; j<choices.length();j++)
			{
				if(choices.getJSONObject(j).optBoolean("is_right"))
				{
					correctAnswer =choices.getJSONObject(j).optString("label");
					//Print the correct answer label
					System.out.println("Correct Answer "+correctAnswer);
				}
			}
		}
		Thread.sleep(3000);
		//Find the correct answer label and click on it
		WebElement correctOne = driver.findElement(By.xpath("//div[@class='label' and text()='"+correctAnswer+"']"));
		correctOne.click();
		Thread.sleep(5000);
		
		//Move out of the WEBVIEW and switch to NATIVE application
		driver.context((String) contextNames.toArray()[0]);
		Thread.sleep(5000);
		
		//Click on the submit button
		WebElement submitBtn = driver.findElement(By.id("haygot.togyah.app.debug:id/skipLayout"));
		submitBtn.click();
		
		//Find the next button and click on it
		WebElement next = driver.findElement(By.id("haygot.togyah.app.debug:id/btn_next"));
		next.click();

}
catch(IOException e)
{
e.printStackTrace();
}