In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to write java automated test scripts". In daily operation, I believe many people have doubts about how to write java automated test scripts. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to write java automated test scripts". Next, please follow the editor to study!
Create a webdriver object
Use the following code to create a webdriver object for the next operation.
WebDriver driver = new ChromeDriver ()
Start with the positioning element
There are eight positioning methods for Selenium, including id positioning, name positioning, class positioning, tag positioning, link positioning, partial positioning, css positioning and Xpath positioning. Among them, Xpath positioning is widely used. The editor then uses Xpath positioning to demonstrate:
Driver.findElement (By.xpath ("/ / * [@ id='nav'] / div/ul/li [2]") .click ()
Under driver, there is a findElement () method to get a html element, which you can click using the click () method after getting this element. You can use By.xpath () to select Xpath in the findElement () method. For tutorials on Xpath, please refer to the Xpath tutorial.
Browser control / / maximize the window driver.manage (). Window (). Maximize (); / control page back driver.navigate (). Back (); / / control browser forward driver.navigate (). Forward (); / / control page refresh driver.navigate (). Refresh (); / / Jump to the specified page driver.get ("https://www.yisu.com/tutorial");" Mouse event WebElement search_setting = driver.findElement (By.xpath ("/ / * [@ id='u'] / a [2]")); / / use Xpath to navigate to the setting button of Baidu search page Actions action = new Actions (driver); / / create a new action object action.clickAndHold (search_setting). Perform () / / use the clickAndHold of the action object to hover over the target / / you must add perform () to execute the code action.contextClick (search_setting). Perform (); / / right-click the target object action.doubleClick (search_setting). Perform (); / / double-click the target object keyboard event WebElement search_text = driver.findElement (By.id ("kw")) / define a webelement element named search_text to hook search_text.sendKeys to the element whose page id is kw ("Java"); / / use the sendkeys of search to send the string "java" search_text.clear () to the element linked to it; / / clear the simulated text of this element and wait for try {Thread.sleep (2000) / / you can suspend a thread temporarily by using the sleep method, but you must wrap} catch (InterruptedException e) {e.printStackTrace ();} with a try-catch statement: import org.openqa.selenium.*;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.interactions.Actions Public class test {public static void main (String [] args) {/ * * if the error says that the Chromedriver cannot be found (that is, the driver is not equipped with an environment variable), use the following code * System.setProperty ("webdriver.chrome.driver", "Chromedriver's driver path"); * / / create a webdriver object WebDriver driver = new ChromeDriver () / / use the webdriver object to access w3Cschool driver.get ("https://www.yisu.com/"); / / use the Xpath method to get the location of an element on the page, use the click method to click on the element driver.findElement (By.xpath (" / * [id='nav'] / div/ul/li [2] ")) .click (); driver.manage () .window () .maximize () / / maximize the window driver.navigate (). Back (); / / Control page back driver.navigate (). Forward (); / / Control browser forward driver.navigate (). Refresh (); / / Control Page Refresh driver.get ("https://www.baidu.com/");// jumps to Baidu WebElement search_text = driver.findElement (By.id (" kw ") / / define a webelement element named search_text to hook search_text.sendKeys ("W3Cschool") to an element whose page id is kw; try {Thread.sleep (2000); / / you can temporarily suspend a thread using the sleep method, but you must wrap} catch (InterruptedException e) {e.printStackTrace () with a try-catch statement } / / use the sendkeys of search to send the string "java" search_text.clear () to the element linked to it; / / clear the simulation text of this element WebElement search_setting = driver.findElement (By.xpath ("/ / * [@ id='u'] / a [2]")) / / use the setting button Actions action = new Actions (driver) of Xpath to navigate to Baidu search page; / / create a new action object action.clickAndHold (search_setting) .perform (); / / use the clickAndHold of action object to hover over the target / / you must add perform () to execute the code action.contextClick (search_setting) .perform (). / / right-click the target object action.doubleClick (search_setting). Perform (); / double-click the target object driver.quit (); / / close the browser object}} so far, the study on "how to write java automated test scripts" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.