"CL" Selenium instance one: Open Baidu, get fourth title

Source: Internet
Author: User

/* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.