1. Introduction
The first few macro introduced two (java and maven) environment to build and browser startup method, this article macro will introduce the first automated test script. The front of the environment are built successfully, the browser is also driven successfully, then we are not in a hurry to learn other content, the first macro to build a good environment to create the first complete automated test script, so that the partners or children in advance to feel the feeling, but also in order to stimulate everyone's interest in learning.
Hong's personal experience is: automation scripting is relatively easy, the biggest difficulty to go how to write test assertions. Automation testing, the most important still falls on the test above, rather than automation, automation is only a means. Assertions are written in a way that tests whether a test engineer has considered all aspects, reflecting the level of your use case writing.
2. Test cases
Many children's shoes or partners may have such a misunderstanding: automated testing is not required to design test cases. In fact, it is not it is also necessary to design test cases, and then according to the use of scripting and assertion of the case, but the use of the case in the form of code, and the machine happens to be able to recognize the code, the code will run up, in fact, is in the implementation of your use case, but by the machine to help you automatically. Well, cut the crap and start talking about the macro to do today's test is: open Baidu, enter Beijing - Hong search, verify that the open link has no Beijing - Hong blog park link.
Test case: open the Baidu home page, search: Beijing - Hong brother, and then check the search list, there is no Beijing - Hong brother blog garden link. Subsequent articles in order to avoid unnecessary trouble and errors, Hong are in the maven built environment for practical demonstration.
2.1 Steps
1. Launch the browser
2. Open the Baidu home page:
3. Determine whether this page is a page we know in advance
4. Position the search input box, record the id of the input box element positioning expression: #kw
5. Locate the search submit button (Baidu), get the id location expression: #su
6. In the search input box enter: Beijing - Hong, point Baidu this button!
7. In the search results list to determine whether the existence of Beijing - Hong brother blog garden this link
8. Exit the browser to end the test
2.2 Code design
2.3 Reference code
package ; import ; 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 automation testing - 5 - create the first automation script (detailed tutorial) * * July 12, 2024 */ public class LaunchChrome { @SuppressWarnings("deprecation") 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 ("/"); //Determine whether the title is Baidu, you will know try{ String baidu_title = "Baidu, you know."; assert baidu_title == (); ("Test Pass"); }catch(Exception e){ (); } //Locate the search input box and type in Beijing-Hongo ("#kw").type("Beijing - Hong"); //Locate the Search Submit button (Baidu). Click ("#su").click(); //Here the element XPath expression is used to determine that the element is displayed in the results list, thus determining that this link to Selenium's official website is displayed in the results list. Locator ele_string=("//*[@id='1']/div/div[1]/h3/a"); String ele_string1 = ele_string.innerText(); (ele_string1); try{ if(ele_string1.equals("Beijing - Hong - Blog Park")){ ("Testing is successful!"); } }catch(Exception e){ (); } //Close page (); //Close browser (); } } }
2.4 Running the code
1. Run the code, right click Run AS->java Application, you can see the console output, as shown below:
3. Summary
3.1 Difference between equals and ==
Macro here briefly, more detailed you can Baidu check. On the issue of determining whether two strings are equal. In programming, the usual expression to compare whether two strings are the same is "==", but you can't write it like that in java. In java, the expression used is equals().
precedent:Astrings andBComparison with strings: if((B)){ } come (or go) backtrue maybefalse.
The equals method of String is used to compare whether two strings are equal. Since strings are object types, you can't use a simple "==" judgment. Instead, use equals to compare the contents of two objects.
Note: equals() compares the contents of the objects (in case sensitive format), but if you use "==" to compare two objects, you are comparing the memory addresses of the two objects, so they are not equal. Even if their contents are equal, the memory addresses of the different objects are not the same.
Well, it's getting late today and the first Java+Playwright automated test script is shared here, thanks for your patience in reading!