你如何用selenium测试reactjs web应用程序?
发布时间:2020-12-14 18:41:30 所属栏目:资源 来源:网络整理
导读:我有一个使用React的Web应用程序,我正在尝试使用Selenium RC创建一些测试.我发现,当Selenium更改字段的值时,事件未被正确触发.我知道这是一个典型的问题,正如 WebDriver FAQ所证明的那样,我尝试过一些不同的东西,比如使用onFocus而不是onChange并确保焦点被
|
我有一个使用React的Web应用程序,我正在尝试使用Selenium RC创建一些测试.我发现,当Selenium更改字段的值时,事件未被正确触发.我知道这是一个典型的问题,正如
WebDriver FAQ所证明的那样,我尝试过一些不同的东西,比如使用onFocus而不是onChange并确保焦点被改变进出,使用sendKeys()vs type(),以编程方式调用该事件,以及我可以在网上找到的任何其他建议,但我无法让它工作.
作为我正在尝试做的一个简单示例,我删除了评论框反应示例.我有一个textarea输入框和一个div应该用textarea的值更新. Selenium可以更新textarea,但是当它发生时div不会改变. 这是我的reactjs代码: /** @jsx React.DOM */
var MarkdownEditor = React.createClass({
getInitialState: function() {
return {value: 'Original Text'};
},handleChange: function() {
this.setState({value: this.refs.textarea.getDOMNode().value});
},render: function() {
return (
<div className="MarkdownEditor">
<h3>Input</h3>
<textarea
onChange={this.handleChange}
ref="textarea"
defaultValue={this.state.value} />
<h3>Output</h3>
<div
className="content"
id="content2",dangerouslySetInnerHTML={{
__html: this.state.value
}}
/>
</div>
);
}
});
React.renderComponent(<MarkdownEditor />,document.getElementById('content'));
这是我目前的Selenium测试用例: import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.regex.Pattern;
import java.util.Properties;
import junit.framework.TestCase;
public class textTest extends TestCase {
protected Selenium selenium;
@Before
public void setUp() throws Exception {
Properties sysProps = System.getProperties();
String browser = sysProps.getProperty("browser.property");
selenium = new DefaultSelenium("localhost",4444,"*chrome","http://localhost/");
selenium.start();
}
@After
public void tearDown() throws Exception {
selenium.stop();
}
@Test
public void testText() throws Exception {
selenium.open("/reactTest/index.html");
assertEquals("Original Text",selenium.getText("id=content2"));
selenium.focus("id=content2");
selenium.type("css=textarea[value="Original Text"]","xxxxx");
selenium.focus("id=content");
selenium.runScript("$('#tatest').trigger('change')");
assertEquals("xxxxxOriginal Text",selenium.getText("id=content2"));
Thread.sleep(80000);
}
}
编辑:代码现在可在https://github.com/ilionblaze/reactTest获得 必须有一些方法来使这项工作! 解决方法
在React TestUtils插件中尝试模拟:
React.addons.TestUtils.Simulate.change(document.getElementById('your-id-here'))
见http://facebook.github.io/react/docs/test-utils.html#simulate 它还要求您切换到包含插件的React文件:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
