This afternoon dealt with a difficult selenium+ Pytthon case submitted to our testing Technical committee by a UI automation engineer. After some thinking, the successful solution, and the method is very simple and reliable, the specific code in the last. Let’s take a look at this:
Visit an internal platform login page and the certificate popup window pops up: you must click OK. Otherwise, you can't continue loading the login page. After you manually click once, closing the browser will still pop up the next time you open it. You want your code to automatically hit OK every time you close the popover and continueCopy the code
For mature testing, it is necessary to first think about several possible ways to solve this problem in your brain, and then quickly judge the difficulty, complexity, degree of trouble, probability of success, subsequent maintenance of each method. First of all, there are two general directions of thinking:
1. Set the browser orientation so that the browser never has to pop up the certificate window. 2. In the automated code, make sure to automatically click OK after each visit to close the popover.Copy the code
Start with Method 1: Start with the browser itself. But the author of a variety of browser certificate Settings is not proficient, even Baidu do not know how to search. And it’s not just one browser, Google/Firefox/IE, and so on. Switching computers, or deploying to a server, is a problem. And based on my own experience, it may take all afternoon, but ultimately it is not successful. So this way is feasible, but the success rate is too low, priority last. So immediately think of method 2: do it from the code, which is cross-platform and doesn’t vary from machine to machine. It’s convenient, fast and saves time and effort.
Approach 2: There are several sub-ideas for starting from code
1. Use the alert.accept() method, which is a no-brainer for automation engineers. This time it’s not a simple Alert popup, but a complex certificate picker. So this route is not feasible. 2. Use autoIt3: Autoit3 was first known by Selenium engineers primarily because it can handle file upload boxes, but the author’s current situation is: Autoit3 is not installed on the computer, and the computer is a MAC, so it is basically unusable. Even if you switch to Windows, you need to find a safe download address, download it, study cracking it, record a script, package it as.exe, and run it in python code. It’s gonna be dark when this process is over. The speed of the program is also limited by the startup speed of the.exe. So it’s a viable option, but not a priority. 3. Use selenium’s mouse/keyboard operation to force ok or Enter. This method is simple, reliable and fast, which is the best choice. So the author immediately carried out a feasibility study. This approach works only if the browser is not buffered and Python code can continue executing after the popup. However, it turns out that during the whole time when the certificate popup window appears, the browser has been buffering around and around, not to mention clicking enter and moving the mouse, even a print cannot be executed. Therefore, a new problem is introduced in this idea: how to click Enter after the popover appears. Solutions include:
1. Use timeout to force driver.execute_script("window.stop()") to stop the page with jsCopy the code
When this method is used, it does stop the page and the program hits Enter to close the certificate popup, but as a result, the browser stops loading. Landing page all things are not out, equivalent to you in order to take out the bullet of the injured person, the person to open the intestines broken the solution plane, the bullet is taken out, the person is not ~
Eventually the author begins to think in terms of another seed:
2. Use multithreading, create a new younger brother to help you click enterCopy the code
This method is to create a child thread, what does the child thread do? Just wait two seconds and hit Enter. It’s that simple. When the main thread goes to the driver.get() method to open the web page, the child thread starts. The main thread then causes the browser to pop up the certificate selection window. After waiting just 2 seconds, the child thread also presses Enter to make the browser certificate selection popup successfully confirm and close, and then the child thread dies. The browser successfully loads the login page, and the main process starts to enter the user name/password. The driver.get() method is not used. Instead, the zs_get() method is called. Why do you have to wait for 2s up there, because the child thread is like your little brother, but a blind person, who can only count to 2 seconds and then swing a knife. If you do not wait, it will cause the subprogram to press Enter first, and the main process will cause the browser to pop up the certificate selection window, which will not be closed. So here’s the original code:
def zs_get(url): The from selenium.webdriver.com mon. Keys import keys # import keys methods, there are many here just for a import threading # import multithreaded module def press_enter () : Sleep (2) #ActionChains(driver).send_keys(key.enter).perform() : Note that you must use a third-party interface tool here, T = threading.thread (target=press_enter) # initthread t.setdaemon (True) driver.get(url) #drver T.s tart # () the child thread start zs_get # (" http://xxxx/xxxx/xxxx.com ") this method is called to replace the driver. The get () can automatic processing the above problemCopy the code
In fact, the author also thought of a lot of methods, but in the test of this directly successful, so the follow-up method did not test and too much thinking. Precious is not only this method, but in the face of a strange problem, to be fearless, not give up, believe that there is no solution to the problem, and then through the scientific method step by step with the shortest time, to determine the best method. Interested partners can leave a message to discuss.