Using selenium to detect JS error

Source: Internet
Author: User
Tags logentries

Background: Received a demand, want to detect whether the page can detect JS error, what is JS error, as shown in the console, using the console, if there is JS error, there will be errors

How to detect, simple version of the operation, open a URL, use manage to get the browser's log, so that will print out this page to get the content

@Testpublic void Test2 () {
Op.loopget ("https://www.rosewholesale.com/cheapest/chic-embossing-braid-5-pieces-3027854.html", 50, 3, 60); This is the way to open the page, just the package logentries logentries = Driver.manage (). Logs (). get (Logtype.browser); for (LogEntry entry: logentries) {log.loginfo (" " +entry.getlevel () + " " +entry.getmessage ());}

  In the log will print the content contains sever and Waring,server is JS error

Own understanding of the principle: the actual use of driver in the log information, and then to filter, you can see in the source code Logtype.browser more than this one, but also to obtain the client, and other, such as you will find it really start driver, each time will record his log, Our principle is actually using his logs to sift through the data we need.

But the real scene, it is often impossible to detect a URL, you need to open a lot of URLs to detect, see a URL contains what JS has a problem, there will be a pit, because driver will always record the page error, so when the page opened, enter multiple URLs, filter, Will repeat the previous data in the filter, that each time to get the corresponding URL of the log, now think of 2 ways to solve:

1. Each time the instance driver, let him as a new object, we get the data is also a URL corresponding to the log, but this time is too long, poor performance

2. Tangled for a long time, change a train of thought, get a log object of a URL, take out the content, in the Get other new URL, determine whether the log has value, has the value to delete, so that each time you open the URL will get the latest URL

/** * @ Test point: Check if JS has an error * @ Verification point: If there is an error in the incoming URL, return an error message @param urllist incoming URL @param timeout page timeout @param looptimes Number of times @param timeoutdefault default load page time @return error URL and error message * @ Remarks: hashmap<string,list<string>> * @author Z Hangjun * @date May 24, 2018 @ Modify Instructions */public hashmap<string, list<string>> checkjssevere (list<string> ur llist, int timeout, int looptimes, int timeoutdefault) {list<string> levellist; hashmap<string, list<string>> severelevel = new hashmap<string, list<string>> (); Logentries logentries = Driver.manage (). Logs (). get (Logtype.browser); for (int i = 0; i < urllist.size (); i++) {try {if ( Logentries.getall (). IsEmpty () = = False) {//Determine if there is content Logentries.iterator (). remove ();//have content to delete}} catch (Exception e) {} String Get_url = Urllist.get (i); On re-deleting loopget (Get_url, timeout, looptimes, timeoutdefault); levellist = new arraylist<string> (); logentries = Driver.manage (). Logs (). get (Logtype.browser); for (LogEntryentry:logentries) {Object level = Entry.getlevel (); String Getlevel = string.valueof (Level), if (Getlevel.equals ("SEVERE")) {Levellist.add (Entry.getmessage ());}} Severelevel.put (Get_url, levellist);} return severelevel;}

  

Using selenium to detect JS error

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.