Selenium软件测试-判断Ajax异步输出

现在大部分系统采用前后端分离技术实现,前端操作通常使用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();
  }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值