1. Operate the browser window
PackageCn.china;Importorg.testng.annotations.Test;ImportOrg.testng.annotations.BeforeMethod;Importorg.openqa.selenium.Dimension;ImportOrg.openqa.selenium.Point;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.chrome.ChromeDriver;ImportOrg.testng.annotations.AfterMethod; Public classApiTest2 { PublicWebdriver Driver; String BaseUrl; @Test Public voidBrowserwindow () {BASEURL= "Http://www.sogou.com"; Driver.navigate (). to (BASEURL); Driver.manage (). window (). SetPosition (NewPoint (50, 50)); Driver.manage (). window (). SetSize (NewDimension (500, 500)); System.out.println (Driver.manage (). window (). getPosition ()); System.out.println (Driver.manage (). window (). GetSize ()); Driver.manage (). window (). Maximize (); } @BeforeMethod Public voidBeforemethod () {System.setproperty ("Webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); Driver=NewChromedriver (); } @AfterMethod Public voidAftermethod () {Try{Thread.Sleep (3000); }Catch(interruptedexception e) {e.printstacktrace (); } driver.quit (); }}
2. Get page Source Resources
Public void Pagesource () { BaseUrl= "http://www.sogou.com"; Driver.navigate (). to (BASEURL); String title=driver.gettitle (); Assert.assertequals ("Sogou search engine-Internet from Sogou start", title); System.out.println (title); String page=driver.getpagesource (); Assert.asserttrue (Page.contains ("shopping")); SYSTEM.OUT.PRINTLN (page); String Url=driver.getcurrenturl (); Assert.assertequals ("https://www.sogou.com/", URL); System.out.println (URL);
3, page refresh, simulation click the browser's forward and backward functions
@Test Public void page () { BaseUrl= "http://www.sogou.com"; Driver.navigate (). to (BASEURL); Driver.navigate (). to ("http://www.dangdang.com"); Driver.navigate (). back (); Driver.navigate (). Forward (); Driver.navigate (). Refresh ();
4, for the input box part of the operation
@Test Public void page () { BaseUrl= "http://www.sogou.com"; Driver.navigate (). to (BASEURL); Webelement input=driver.findelement (by.id ("Query")); Input.clear (); Input.sendkeys ("test engineer Specifies input"); Driver.findelement (By.id ("STB")). Click ();
5, analog keyboard, mouse operation page
@Test Public voidpage () {BASEURL= "Http://www.sogou.com"; Driver.navigate (). to (BASEURL); Webelement input=driver.findelement (by.id ("Query")); Input.sendkeys ("Selenium Webdriver API"); Actions Action=NewActions (driver); Action.contextclick (Input). perform ();//Mouse Right-clickAction.doubleclick (Input). perform ();//Mouse Double-clickInput.sendkeys (Keys.chord (keys.control+ "a"));//simulating keyboard combination operations using the SendKeys methodInput.sendkeys (Keys.chord (keys.control+ "C")); Webelement InputBox=driver.findelement (By.id ("InputBox")); Inputbox.sendkeys (Keys.chord (Keys.control+ "V")); Action.keydown (Keys.control);//using the Actions object to simulate keyboard actionsAction.keyup (Keys.control); Action.keydown (Keys.shift). SendKeys ("ABCdef"). Perform (); //Mouse HoverWebelement link1=driver.findelement (By.xpath ("//a[@id = ' Link1 ']")); Action.movetoelement (LINK1). Perform ();
Webdriver--api (Java Edition) the first part