/* When creating the class is TestNG class*/
Package Selenium_lassen;
Import static org.junit.assert.*;
Import Java.util.concurrent.TimeUnit;
Import Org.junit.After;
Import Org.junit.Before;
Import Org.junit.Test;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import Org.openqa.selenium.firefox.FirefoxDriver;
public class Case4 {
Webdriver driver;
@Test
public void Test () {
System.setproperty ("Webdriver.firefox.bin", "D:\\ruanjian\\firefox\\azml\\firefox.exe");
Driver=new firefoxdriver ();//opening firefox; open Firefox
Driver.get ("http://www.baidu.com");//Turn on the Baidu open the URL
Driver.findelement ("By.id (" kw ")). SendKeys (" GitHub ");//Enter the search keyword" github "; input searches keyword
Driver.findelement (By.id ("su")). Click ();//Tap the search button click the
Driver.manage (). Timeouts (). Implicitlywait (Ten, timeunit.seconds);//etc page load, 10 seconds does not load the success of the report timeout. Waiting for ten seconds
String aresult=driver.findelement (By.xpath (".//*[@id = ' 4 ']/h3/a")). GetText ();//Take the title of the fourth search result. Get the text of 4th search result
System.out.println ("title is:" +aresult);
Assert Aresult.contains ("GitHub");//Make Assertion assertion
Driver.findelement (By.xpath (".//*[@id = ' 4 ']/h3/a")). Click ();//Open the fourth search result. Open the 4th search result on Baidu
Driver.manage (). Timeouts (). Implicitlywait (Ten, timeunit.seconds);//etc page load, 10 seconds does not load the success of the report timeout. Waiting for ten seconds
Get the handle of all windows, then switch to the new window
For (String winHandle:driver.getWindowHandles ()) {
Driver.switchto (). window (Winhandle);
}
String atitle=driver.gettitle ();//Take the title of the new window
System.out.println ("Current Widnow title is:" +atitle);//Play it out and see
Assert Atitle.contains ("GitHub");//Assertion
}
}
"CL" Selenium instance one: Open Baidu, get fourth title