Nine, tool class, start all remote Services browser
Create a Java class in the Utils package: launchallremotebrowsers
Package Com.lingfeng.utils;import Java.net.malformedurlexception;import Java.util.hashmap;import Java.util.iterator;import Java.util.map;import Java.util.map.entry;import Java.util.set;import Org.dom4j.documentexception;import Org.openqa.selenium.webdriver;import Com.lingfeng.staticpage.remoteserveropenhomepage;import com.lingfeng.utils.remoteserverinit;/** * This class provides a way to start all remote server-side browsers * @author Ling * */public class Launchallremotebrowsers {private static String drivername;/** * This method is browser-initiated Dynamic entry, enabling this method means that the browser on all remote server * @return map<string,thread> contains the browser name and all threads that are running. Key is the browser name and value is the running thread. */public static map<string,thread> Launch () {//store all running browser names and threads map<string,thread> Threadmap = new HashMap <string, thread> (); try {//Initialize browser configuration information remoteserverinit.launchbrowser ();//Get initialization of all browser-initiated drivermap<string, webdriver> Drivermap = Remoteserverinit.getalllaunchdrivermap ();//Get the driver to start the browser and nameset<entry<string, webdriver>> Driverinfoset = Drivermap.entryset (); iterator<entry<string, webdriver>> driveriterator = Driverinfoset.iterator (); while (Driveriterator.hasnext ()) {Thread CurrentThread = Launchthread (driveriterator); Threadmap.put (drivername, CurrentThread);} return threadmap;} catch (Malformedurlexception e) {e.printstacktrace ();} catch (Documentexception e) {e.printstacktrace ();} return threadmap;} /** * Multithreaded call, start a thread * @param driveriterator * @return Thread currently started threads */private static thread Launchthread (iterator<entry <string, webdriver>> driveriterator) {entry<string, webdriver> driverentry = DriverIterator.next (); drivername = Driverentry.getkey (); Webdriver Webdriver = Driverentry.getvalue (); Remoteserveropenhomepage homepage= New Remoteserveropenhomepage (Webdriver,drivername); Thread t = new thread (homepage); T.start (); return t;}}
Ten, start testing class come on
To create a Java class in the start package: starttest
Package Com.lingfeng.start;import Java.util.iterator;import Java.util.map;import java.util.map.entry;import Java.util.set;import Com.lingfeng.utils.launchallremotebrowsers;import Com.lingfeng.utils.remotethreadstatusmonitor;public class Starttest {public static void start () {map<string, Thread > allrunningthreads = Launchallremotebrowsers.launch (); set<entry<string,thread>> Runningthreadsset = Allrunningthreads.entryset ();Iterator<Entry< String,thread>> it = Runningthreadsset.iterator (); int threadcount=0; Record the number of startup threads while (It.hasnext ()) {threadcount++; entry<string, thread> Entry = It.next ();//start the monitor thread and monitor the state of the running thread. Remotethreadstatusmonitor C = new Remotethreadstatusmonitor (Entry.getkey (), Entry.getvalue ()); Thread t = new thread (c); T.start ();} The JVM exits when the number of dead threads equals the number of startup threads. while (Remotethreadstatusmonitor.getdeadthreadcount ()!=threadcount) {//must be added to this thread's sleep method, otherwise the main thread will not end. try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ()}} }}
Debugging this class requires a problem, perhaps the younger brother's Java Foundation or not solid, the problem is as follows:
The JVM exits when the number of dead threads equals the number of startup threads. while (Remotethreadstatusmonitor.getdeadthreadcount ()!=threadcount) {//must be added to this thread's sleep method, otherwise the main thread will not end. try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ()}}
In this code above, if I do not join the sleep method, and nothing in the while loop, the main process will not end, plus it is OK, all the work of the thread end, the main process will naturally end, there is no reason to understand. If there is a master to see this article, hope to be able to give doubts, grateful!
Xi. JUnit Test
JUnit package, create a test class directly.
Package Com.lingfeng.junit;import Org.junit.test;import Com.lingfeng.start.starttest;public class JunitTest {@ testpublic void Teststart () {Starttest.start ();}}
After running, print the following results:
Note: All Java packages and classes are created with the following directories:
Can be compared by itself.
At this point, Selenium's multi-threaded startup grid Distributed test framework encapsulation, encapsulation completed.
SOURCE jar:http://yunpan.cn/ckaclzvv4ba3q access Password c81b
Continuous attention, will take the time to continue to encapsulate a variety of tool classes, as well as the log, reporting framework, such as encapsulation.
Selenium multi-threaded startup grid Distributed test framework package (IV)