I want to create a txt file which stores the details against a certain section in my app. The app is open in Emulator. It copies the entries and pastes it but only one iteration is done. I have to run the test over and over to find any bugs in this section. I am attaching my code below.
package Test.Appium;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
public class Test1 {
@SuppressWarnings("unused")
@Test
public void LogDateTest() throws URISyntaxException, InterruptedException, IOException {
UiAutomator2Options options = new UiAutomator2Options();
options.setDeviceName("Emulator"); // emulator
AndroidDriver driver = new AndroidDriver(new URI("http://000.0.0.0:4723/").toURL(), options);//add ur ip
try {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
// Locate the SE app which is already open
WebElement app = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("")));//xpath of my app
app.click();
// Locate the "Last updated" section and get the time and date
WebElement lastUpdatedElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(");//id of the section that im storing in my txt file
if (lastUpdatedElement != null) {
String lastUpdatedText = lastUpdatedElement.getText();
System.out.println("Last Updated: " + lastUpdatedText);
// Log the time and date to a file
logLastUpdated(lastUpdatedText);
} else {
System.out.println("Element not found: ");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// Close driver after test execution
if (driver != null) {
driver.quit();
}
}
}
// Method to log the last updated information to a file
public void logLastUpdated(String lastUpdated) throws IOException {
String logFilePath = "";//txt file location
try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFilePath, true))) {
writer.write(lastUpdated);
writer.newLine();
System.out.println("Logged: " + lastUpdated);
} catch (IOException e) {
e.printStackTrace();
`System.out.println("Failed to log data to file: " + e.getMessage());
}
}
}
I am expecting to only run the code once and it stores at least 60 entries. I want the code to run for a minimum of 60 mins. The scenario is to store the data of this specific section in the txt file. It should run for an hour and store the values after each minute. I want to identify if there is any missing values or if there is an error in updating the values.
leftcode is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.