Capturing Browser Errors and Logs in iOS Web/Hybrid Apps
We & # x27; ve talked a few times about automate and apps with Appium. One characteristic of such apps is the power to write information to the browser console. Whether or not this console is seeable to the end exploiter is irrelevant -- -app developer ofttimes announce interesting information about the province of an app in the form of browser console message. It would be useful to be able to retrieve this info as part of our test, for two main reasons: Something closely related to the second purpose is the demand to capture JSerrors; it & # x27; s extremely useful to detect whether the web/hybrid app has cast any errors during the course of a test! Using a unproblematic slight hack, it & # x27; s actually quite leisurely to get any JS error lumber to the browser console. We simply have to run this little bit of JS in the page: For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users. This fundamentally tells the browser to direct info about any mistake it detects on the page to the console. Of class, all of this is moot if there & # x27; s no way to use Appium to retrieve this info. Happily, there is! Let & # x27; s take a look at how to retrieve the browser logarithm for Safari (or any other iOS hybrid app). We commence by using the log retrieval mechanics built into the Appium client (by way of the Selenium node include within), and specifying a particular, customlog type safariConsole: In Java, this will return aLogEntriesobject which contains a set of (you guessed it) log entries: one per console message which has be received since the last clip we retrieved the introduction. Each entry has certain information associated with it: amessage, a level and a timestamp. In the case ofsafariConsolelogs, the message is itself a JSON string, which contains lots of info (the console substance itself, a stack trace, and so on). This means that we & # x27; ll typically need to convert the content from its JSON string format into an aim we can work with in our code. Let & # x27; s say we wanted to publish out the numeric timestamp, the severity level, and the message of everything that & # x27; s been logged to the console. We can do so habituate the following code: Essentially, what we & # x27; re doing is going through each entry in the list of debut, turning the substance into a key/value brace so we can extract specific data from it, and using some of the general entry API methods as well (to get the clip and severity point of the substance). Of course, this is just one exemplar of how we could use thedriver.manage () .logs ()feature. We could do all kinds of thing with it. But when would we want to do something like above? It would sit very naturally in some sort of teardown method. Instead of write to stdout, we could write the console messages to a file and salve it along with other test artifacts in case we need it after on to envision out why a test failed. We could too use thewindow.onerrorhack I exhibit above, to make sure that any JS fault are brought to our attention; we could even be real strict with our developer and automatically fail a test if anySEVERE-level log unveiling come through! We don & # x27; t even need our developers & # x27; permission to set up this slight error handler; we can do it ourselves usingexecuteScriptat the kickoff of our test: (2 caveats hither with this simplistic hack: first, we & # x27; ll need to run this slight scriptafterthe page has loaded, which means we won & # x27; t catch erroneousness that happen on page load. Second, we & # x27; d overwrite any other handler attach to theonerror case, which might break the intended conduct of the app if such a handler was already in use). At any rate, apply thesafariConsolelog eccentric, we & # x27; re able to get all variety of occupy information from the browser console, whether in Safari or still intercrossed apps. Have a look at theentire code samplingto see how I & # x27; ve put everything together in the setting of a real-world illustration: Lead, Content Marketing, HeadSpin Inc. Piali is a dynamic and results-driven Content Marketing Specialist with 8+ days of experience in crafting occupy tale and marketing collateral across various industriousness. She excels in collaborating with cross-functional teams to develop innovative content strategies and deliver compelling, authentic, and impactful message that resonates with target hearing and enhances marque genuineness. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts..png)



Capturing Browser Errors and Logs in iOS Web/Hybrid Apps
AI-Powered Key Takeaways
Check out:
window.onerror = console.error.bind (console);Read:
driver.manage () .logs () .get (`` safariConsole '');for (LogEntry entry: driver.manage () .logs () .get (`` safariConsole '')) {HashMapAlso read:
driver.executeScript (`` window.onerror=console.error.bind (console); '');Also check:
importation io.appium.java_client.ios.IOSDriver; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import org.junit.After; import org.junit.Before; import org.junit.Test; significance org.openqa.selenium.json.Json; import org.openqa.selenium.logging.LogEntry; import org.openqa.selenium.remote.DesiredCapabilities; public class Edition037_iOS_Web_Console {private IOSDriver driver; @ Before public void setUp () cast MalformedURLException {DesiredCapabilities capabilities = new DesiredCapabilities (); capabilities.setCapability (`` platformName '', `` iOS ''); capabilities.setCapability (`` platformVersion '', `` 11.4 ''); capabilities.setCapability (`` deviceName '', `` iPhone 8 ''); capabilities.setCapability (`` browserName '', `` Safari ''); driver = new IOSDriver < > (new URL (`` http: //localhost:4723/wd/hub ''), capabilities);} @ After public vacancy tearDown () {if (driver! = null) {for (LogEntry introduction: driver.manage () .logs () .get (`` safariConsole '')) {HashMapPiali Mazumdar
Capturing Browser Errors and Logs in iOS Web/Hybrid Apps
4 Parts
-1280X720-Final-2.jpg)
Regression Intelligence practical guide for advanced users (Part 3)
-1280X720-Final-2.jpg)
Regression Intelligence pragmatic guide for advanced users (Part 4)
Discover how HeadSpin can empower your business with superior testing capabilities







Discover how HeadSpin can empower your job with superior testing capabilities
Discover how HeadSpin can gift your business with superior try capabilities
Connet Now


Automate This With SUSA
Test Your App Autonomously







.png)












