Location>code7788 >text

A First Look at the "Just Asked" Series - Java+Playwright Automated Testing - 3 - Launching the Browser (Detailed Tutorial)

Popularity:304 ℃/2024-10-18 14:01:44

5.1 Code Design

5.2 Reference code

package bjhg_playwright;

import ;
import ;
import ;
import ;

/**
 * @author Beijing-Hong
 *
 * @public number:Beijing Hong (WeChat search, pay attention to Hong, unlock more testing dry goods in advance)
 *
 * "Just Asked" series of first peek - Java + Playwright automated testing - 3 - start the browser (detailed tutorial)
 *
 * July 09, 2024
*/
public class LaunchWebkit {
    
    public static void main(String[] args) {
        try (Playwright playwright = ()) {
          Browser browser = ().launch(new ().setHeadless(false).setSlowMo(50));
          Page page = ();
          ("/");
          (());
          ();
        }
      }

}

5.3 Running the code

1. Run the code, right click Run AS->java Application, you can see the console output, as shown below:

2. Run the code after the browser action on the computer side. As shown in the figure below:

6. Key code descriptions

package bjhg_playwright;
import ;
import ;
import ;
import ;

/**
 * @author Beijing-Hong
 *
 * @public number:Beijing Hong (WeChat search, pay attention to Hong, unlock more testing dry goods in advance)
 *
 * "Just Asked" series of first peek - Java + Playwright automated testing - 3 - start the browser (detailed tutorial)
 *
 * July 09, 2024
*/
public class LaunchChrome {
    
    public static void main(String[] args) {
        try (Playwright playwright = ()) {
          //Using chromium browser, # Browser Configuration, set to launch Chrome in GUI mode (to see the browser UI, pass the headless=false flag when launching the browser. You can also use slowMo to slow down execution.
          Browser browser = ().launch(new ().setHeadless(false).setSlowMo(50));
          //Create page
          Page page = ();
          //Open Baidu in your browser
          ("/");
          //Print title
          (());
          //Close page
          ();
        }
      }

}

Through the above in the code to add comments, I think the partners or children will be better understood, but also know macro in the first article to run the code, why did not see the browser UI it!

7. Summary

Macro because of the previous python, java language and selenium, often encountered headaches is: the browser version and driver version to match the problem, newcomers must pay attention to this problem. But playwright either Java or python language, either novice or old bird do not need to worry about these problems, and today explained and shared very simple, is a simple change of method can start a different browser. Well, today on the three major browsers drive Hong brother to share here, thank you for your patience to read.