Suppose we are verifying device logs also as part of test assertion in mobile app automation. In Android, we can collect device log through adb logcat command. For iOS we can use desired capability to print device log in the console. We can use scripts (or batch files for Windows) for this purpose.
When we want to run the scripts from within our test assertion, we need to create and call method that in turn executes this script. Then we need to create and call another method that does the verification and return boolean value based on verification outcome. We need to feed this second method in our assert statement.
Some examples:
We would implement Android device log check in this blog. You can implement iOS device log check in similar way.
First, we would create a script file to take android device log (last 5000 lines) through adb. Following is the scripts:
Adb_log_command.txt:
#! /bin/bash
cd ~/Downloads/testautomation/appautomation
rm devicelog_android.txt
cd ~
./adb shell logcat -t 5000 > Downloads/testautomation/appautomation/devicelog_android.txt
Next, we would create a LogUtil class with 2 utility methods – one to call the above script and another to search the resulting log file for specific content/text.
package com.taf.testautomation.utilities.logutil;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import static com.taf.testautomation.utilities.excelutil.ExcelUtil.getCustomProperties;
@Slf4j
public class LogUtil {
public static void takeDeviceLog() {
try {
Runtime.getRuntime().exec("./adb_log_command.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean deviceLogCheck(String content) {
try {
String filePath = "devicelog_android.txt";
BufferedReader br = new BufferedReader(new FileReader(filePath));
String strLine = null;
while ((strLine = br.readLine()) != null) {// read every line in the file
if (strLine.contains(content)) {
return true;
}
}
br.close();
} catch (Exception e1) {
log.info(e1.getMessage());
}
return false;
}
}
Now we would plug-in the above methods in our test suite method. You can similarly implement iOS device log check.
@Severity(SeverityLevel.CRITICAL)
@Issue("xxxx")
@DisplayName("xxxx")
@Description("xxxx: Verify that the App Logo is displayed")
@Test
@Order(2)
@Smoke
public void testScenario() {
String tcName = new Object() {
}.getClass().getEnclosingMethod().getName();
log("Test Name" + tcName);
LogUtil.takeDeviceLog();
try {
SoftAssertions.assertSoftly(
softAssertions -> {
softAssertions.assertThat(aboutAppScreen.isAppLogoDisplayed()).as("The App Logo is displayed").isTrue();
}
);
SoftAssertions.assertSoftly(
softAssertions -> {
softAssertions.assertThat(LogUtil.deviceLogCheck("xxxx")).as("Device log shows correct data").isTrue();
}
);
} finally {
testStatus = aboutAppScreen.isAppLogoDisplayed() ? "Passed" : "Failed";
updateTCPassCount();
}
}
- TS360 Engineering Team