selenium-WebDriver API(java)(七)

本篇知识点:

  • 判断页面元素是否存在
  • 使用Title属性识别和操作新弹出的浏览器窗口
  • 使用页面的文字内容识别和处理新弹出的浏览器窗口
  • 操作JavaScript的Alert弹窗
  • 操作JavaScript的confirm弹窗
  • 操作JavaScript的prompt弹窗

判断页面元素是否存在

	//判断页面元素是否存在
		@Test(enabled = false)
	private boolean IsElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			// TODO: handle exception
			return false;
		}
	}

	@Test(enabled = false)
	public void testIsElementPresent() {
		driver.get("http://www.sogou.com");
		if (IsElementPresent(By.id("kw"))) {
			// 如果成功定位元素,则把元素的对象存储到searchInputBox变量中
			WebElement searchInputBox = driver.findElement(By.id("kw"));
			if (searchInputBox.isEnabled() == true) {
				searchInputBox.sendKeys("baidu首页的搜索输入框被成功找到!");
			} else {
				Assert.fail("首页上的输入框元素未被找到");
			}
		}
	}

使用Title属性识别和操作新弹出的浏览器窗口

被测试网页的HTML代码

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>你喜欢的水果</title>
</head>
<body>
<p id='p1'>你唉吃的水果么?</p>
<br><br>
<a href="http://www.baidu.com" target="_blank">baidu 搜索</a>
</body>
</html>

实例代码:

// 使用Title属性识别和操作新弹出的浏览器窗口
	@Test(enabled = false)
	public void identifyPopUpWindowByTitle() {
		driver.get("file:///F:/workspace/WebDriver%20API/indentifyPopUpWindow.html");
		// 先将当前浏览器窗口的句柄存储到parentWindowHandle变量
		String parentWindowHandle = driver.getWindowHandle();
		WebElement baiduLink = driver.findElement(By.xpath("//a"));
		baiduLink.click();

		// 获取当前所有打开窗口的句柄,并把他们存储到一个set容器中
		Set<String> allWindowHandles = driver.getWindowHandles();
		if (!allWindowHandles.isEmpty()) {
			for (String WindowHandle : allWindowHandles) {
				try {
					System.out.println(WindowHandle);
					// 调用driver.switchTo().window(WindowHandle)方法可以把当前的窗口通过句柄为条件切换为对应句柄的窗口
					// 搜索浏览器title属性,比较当前窗口的标题是否与预期的一致
					if (driver.switchTo().window(WindowHandle).getTitle().equals("百度一下,你就知道 ")) {
						// 如果当前切换到的首页就是预期的网页,则在页面的输入框中输入"baidu
						// 首页的浏览器窗口被找到了"这几个关键字
						driver.findElement(By.id("kw")).sendKeys("baidu 首页的浏览器窗口被找到了");
					}
				} catch (NoSuchElementException e) {
					// 如果没有找到浏览器的句柄,则会抛出NoSuchWindowException,打印异常信息。
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		}
		// 切换回最开始打开的浏览器页面
		driver.switchTo().window(parentWindowHandle);
		Assert.assertEquals(driver.getTitle(), "你喜欢的水果");
	}

使用页面的文字内容识别和处理新弹出的浏览器窗口

被测试网页的HTML代码同上。

实例代码:

	// 使用页面的文字内容识别和处理新弹出的浏览器窗口
	@Test(enabled = false)
	public void identifyPopUpWindowByPageSource() {
		driver.get("file:///F:/workspace/WebDriver%20API/indentifyPopUpWindow.html");
		// 先将当前浏览器窗口的句柄存储到parentWindowHandle变量
		String parentWindowHandle = driver.getWindowHandle();
		WebElement baiduLink = driver.findElement(By.xpath("//a"));
		baiduLink.click();

		// 获取当前所有打开窗口的句柄,并把他们存储到一个set容器中
		Set<String> allWindowHandles = driver.getWindowHandles();
		if (!allWindowHandles.isEmpty()) {
			for (String WindowHandle : allWindowHandles) {
				try {
					System.out.println(WindowHandle);
					// 调用driver.switchTo().window(WindowHandle)方法可以把当前的窗口通过句柄为条件切换为对应句柄的窗口
					// 搜索浏览器的源代码,并判断是否包含“百度搜索”这几个关键字
					if (driver.switchTo().window(WindowHandle).getPageSource().contains("百度搜索")) {
						// 如果当前切换到的首页就是预期的网页,则在页面的输入框中输入"baidu
						// 首页的浏览器窗口被找到了"这几个关键字
						driver.findElement(By.id("kw")).sendKeys("baidu 首页的浏览器窗口被找到了");
					}
				} catch (NoSuchElementException e) {
					// 如果没有找到浏览器的句柄,则会抛出NoSuchWindowException,打印异常信息。
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		}
		// 切换回最开始打开的浏览器页面
		driver.switchTo().window(parentWindowHandle);
		Assert.assertEquals(driver.getTitle(), "你喜欢的水果");
	}

操作JavaScript的Alert弹窗

目标是能够模拟单击弹出Alert窗口上的“确定”按钮。

被测试网页html代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>你喜欢的水果</title>
</head>
<body>
	<input id='button' type='button' onclick="alert('这是一个alert弹出框');"
		value='单击此按钮,弹出alert弹出框' /></input>

</body>
</html>

实例代码如下:

// 操作JavaScript的Alert弹窗
	@Test(enabled = false)
	public void testHandleAlert() {
		driver.get("file:///F:/workspace/WebDriver%20API/HandleAlert.html");
		// 使用XPath定位方法,查找被测试页面上的唯一按钮元素
		WebElement button = driver.findElement(By.xpath("//input"));
		button.click();
		try {
			// 使用driver.switchTo().alert()方法获取alert对象
			Alert alert = driver.switchTo().alert();
			// 使用alert.getText()方法获取alert框上面的文字,并断言内容是否和
			// "这是一个alert弹出框"关键字相一致
			Assert.assertEquals("这是一个alert弹出框", alert.getText());
			// 使用Alert对象的accept方法,单击Alert框上的“确定”按钮,关闭alert框
			alert.accept();
		} catch (NoAlertPresentException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

操作JavaScript的confirm弹窗

能够模拟单击JavaScript弹出的confirm框中的“确定”和“取消”按钮。

被测试网页html代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>你喜欢的水果</title>
</head>
<body>
	<input id='button' type='button' onclick="confirm('这是一个confirm弹出框');"
		value='单击此按钮,弹出confirm弹出框' /></input>

</body>
</html>

实例代码如下:

// 操作JavaScript的confirm弹窗
	@Test(enabled = false)
	public void testHandleconfirm() {
		driver.get("file:///F:/workspace/WebDriver%20API/Handleconfirm.html");
		// 使用XPath定位方法,查找被测试页面上的唯一按钮元素
		WebElement button = driver.findElement(By.xpath("//input"));
		button.click();
		try {
			// 使用driver.switchTo().alert()方法获取alert对象
			Alert alert = driver.switchTo().alert();
			// 使用alert.getText()方法获取alert框上面的文字,并断言内容是否和
			// "这是一个confirm弹出框"关键字相一致
			Assert.assertEquals("这是一个confirm弹出框", alert.getText());
			// 使用Alert对象的accept方法,单击Alert框上的“确定”按钮,关闭alert框
			alert.accept();
		} catch (NoAlertPresentException e) {
			// TODO: handle exception
			Assert.fail("尝试操作的confirm框未被找到");
			e.printStackTrace();
		}
	}

操作JavaScript的prompt弹窗

        能够在JavaScript的prompt弹窗中输入自定义的字符串,单击“确定”按钮和“取消”按钮。

被测试网页html代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>你喜欢的水果</title>
</head>
<body>
	<input id='button' type='button' onclick="prompt('这是一个prompt弹出框');"
		value='单击此按钮,弹出prompt弹出框' /></input>
</body>
</html>

实例代码:

	// 操作JavaScript的prompt弹窗
		@Test(enabled = false)
		public void testHandlePrompt() {
			driver.get("file:///F:/workspace/WebDriver%20API/HandlePrompt.html");
			// 使用XPath定位方法,查找被测试页面上的唯一按钮元素
			WebElement button = driver.findElement(By.xpath("//input"));
			button.click();
			try {
				// 使用driver.switchTo().alert()方法获取alert对象
				Alert alert = driver.switchTo().alert();
				// 使用alert.getText()方法获取alert框上面的文字,并断言内容是否和
				// "这是一个confirm弹出框"关键字相一致
				Assert.assertEquals("这是一个prompt弹出框", alert.getText());
				// 使用Alert对象的accept方法,单击Alert框上的“确定”按钮,关闭alert框
				alert.accept();
			} catch (NoAlertPresentException e) {
				// TODO: handle exception
				Assert.fail("尝试操作的prompt框未被找到");
				e.printStackTrace();
			}
		}

猜你喜欢

转载自blog.csdn.net/vikeyyyy/article/details/80181318
今日推荐