现在大部分系统采用前后端分离技术实现,前端操作通常使用Ajax与后台交互,使用Selenium如何捕捉Ajax异步请求的返回值,目前还没有找到直接的方法获取Ajax的返回值,但如果Ajax返回数据没有体现在Dom对象上或者使用提示框显示,可以使用Selenium获取到,如下图在部门页面上点击“添加子部门”,通过Ajax异步提交,判断是否提交成功必须根据Ajax的返回值。
添加子部门调用的JS方法
function addsuborg()
{
var frmdata = $('#frm').serialize();
$.ajax(
{
url:'/orgsubsave',
data:frmdata,
success:function(data){
if(data == true)
{
reloadtree();
alert('保存成功');
}
else
{
alert('保存失败');
}
}
})
}
测试脚本断言
Alert alert = driver.switchTo().alert();
String txt = alert.getText();
alert.accept();
System.out.println("============="+txt);
assertEquals(txt,"保存成功");
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gf</groupId>
<artifactId>SeleniumProj2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SeleniumProj2</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
</project>
全部脚本
package com.gf;
//import org.junit.Test;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class AddOrgTest {
private WebDriver driver;
private Map<String, Object> vars;
JavascriptExecutor js;
@Before
public void setUp() {
driver = new FirefoxDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap<String, Object>();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void test(){
driver.get("http://localhost:6060/");
driver.manage().window().setSize(new Dimension(1382, 744));
driver.findElement(By.id("loginId")).click();
driver.findElement(By.id("loginId")).sendKeys("admin");
driver.findElement(By.id("pwd")).click();
driver.findElement(By.id("pwd")).sendKeys("1");
driver.findElement(By.id("loginbtn")).click();
driver.findElement(By.cssSelector(".accordion-expand")).click();
driver.findElement(By.cssSelector("#\\_easyui_tree_4 > .tree-title")).click();
driver.switchTo().frame(0);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.cssSelector(".tree-hit")).click();
driver.findElement(By.cssSelector(".tree-collapsed")).click();
driver.findElement(By.cssSelector(".tree-title:nth-child(4)")).click();
driver.findElement(By.id("_easyui_textbox_input1")).clear();
driver.findElement(By.id("_easyui_textbox_input1")).sendKeys("Test3");
driver.findElement(By.id("_easyui_textbox_input2")).clear();
driver.findElement(By.id("_easyui_textbox_input2")).sendKeys("1");
driver.findElement(By.xpath("//a[@id=\'btn\']/span/span")).click();
try {
Thread.sleep(2000); //让线程等待3秒钟
} catch (InterruptedException e) {
e.printStackTrace();
}
Alert alert = driver.switchTo().alert();
String txt = alert.getText();
alert.accept();
System.out.println("============="+txt);
assertEquals(txt,"保存成功");
/**
Boolean rtn = new WebDriverWait(driver, 10).until (new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
Boolean result = false;
try {
driver.findElement(By.xpath("//a[@id=\'btn\']/span/span")).click();
result = false;
System.out.println("===================");
} catch(Exception e){
}
return result;
}
});
System.out.println("======"+rtn);
*/
driver.findElement(By.cssSelector(".tree-hit")).click();
driver.findElement(By.cssSelector(".tree-collapsed")).click();
}
}