-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestBaidu.java
81 lines (72 loc) · 2.15 KB
/
TestBaidu.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cn.tjucic.selenium;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class TestBaidu {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
String driverPath = System.getProperty("user.dir") + "/src/resources/driver/geckodriver.exe";
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver();
baseUrl = "https://www.baidu.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testBaidu() throws Exception {
driver.get(baseUrl + "/");
WebElement we = driver.findElement(By.id("kw"));
we.click();
// driver.findElement(By.id("kw")).click();
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("天津大学");
driver.findElement(By.id("su")).click();
assertEquals("天津大学_百度搜索", driver.getTitle());
}
@After
public void tearDown() throws Exception {
// driver.quit();
// String verificationErrorString = verificationErrors.toString();
// if (!"".equals(verificationErrorString)) {
// fail(verificationErrorString);
// }
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}