有什么区别expect(something).toBe(true)
,expect(something).toBeTruthy()
和expect(something).toBeTrue()
?
请注意,这toBeTrue()
是一个自定义匹配器,介绍在jasmine-matchers
其他有用和方便的匹配器,如toHaveMethod()
或toBeArrayOfStrings()
.
问题是通用的,但是,作为一个真实的例子,我正在测试一个元素是否显示在protractor
.在这种情况下我应该使用哪个匹配器?
expect(elm.isDisplayed()).toBe(true);
expect(elm.isDisplayed()).toBeTruthy();
expect(elm.isDisplayed()).toBeTrue();
Run Code Online (Sandbox Code Playgroud) 我应该使用Protractor或Karma进行端到端测试吗?
Angular-seed使用Protractor/Selenium WebDriver进行E2E,但angular-phonecat教程使用业力.
我读到我应该使用Karma进行单元测试,使用Protractor进行E2E,看起来不错,但我想我会在这里要求得到其他开发者的意见.
故事:
在StackOverflow上,我看到用户报告他们无法通过selenium WebDriver"click"命令单击一个元素,并且可以通过执行脚本单击JavaScript来解决它.
Python中的示例:
element = driver.find_element_by_id("myid")
driver.execute_script("arguments[0].click();", element)
Run Code Online (Sandbox Code Playgroud)
WebDriverJS/Protractor中的示例:
var elm = $("#myid");
browser.executeScript("arguments[0].click();", elm.getWebElement());
Run Code Online (Sandbox Code Playgroud)
问题:
为什么在常规WebDriver点击时没有点击"通过JavaScript"?究竟是什么时候发生这种变通方法的缺点(如果有的话)?
我个人使用这种解决方法而没有完全理解为什么我必须这样做以及它可能导致什么问题.
我试图从下拉列表中选择使用量角器进行角度e2e测试的选项.
以下是select选项的代码段:
<select id="locregion" class="create_select ng-pristine ng-invalid ng-invalid-required" required="" ng-disabled="organization.id !== undefined" ng-options="o.id as o.name for o in organizations" ng-model="organization.parent_id">
<option value="?" selected="selected"></option>
<option value="0">Ranjans Mobile Testing</option>
<option value="1">BeaverBox Testing</option>
<option value="2">BadgerBox</option>
<option value="3">CritterCase</option>
<option value="4">BoxLox</option>
<option value="5">BooBoBum</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我试过了:
ptor.findElement(protractor.By.css('select option:1')).click();
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
指定了无效或非法字符串构建信息:版本:'2.35.0',修订版:'c916b9d',时间:'2013-08-12 15:42:01'系统信息:os.name:'Mac OS X' ,os.arch:'x86_64',os.version:'10 .9',java.version:'1.6.0_65'驱动程序信息:driver.version:unknown
我也尝试过:
ptor.findElement(protractor.By.xpath('/html/body/div[2]/div/div[4]/div/div/div/div[3]/ng-include/div/div[2]/div/div/organization-form/form/div[2]/select/option[3]')).click();
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
ElementNotVisibleError:元素当前不可见,因此可能无法与命令持续时间或超时交互:9毫秒构建信息:版本:'2.35.0',修订版:'c916b9d',时间:'2013-08-12 15:42: 01'系统信息:os.name:'Mac OS X',os.arch:'x86_64',os.version:'10 .9',java.version:'1.6.0_65'会话ID:bdeb8088-d8ad-0f49-aad9 -82201c45c63f驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform = MAC,acceptSslCerts = true,javascriptEnabled = true,browserName = firefox,rotating = false,locationContextEnabled = true,version = 24.0,cssSelectorsEnabled = true,databaseEnabled = true,handlesAlerts = …
我正在尝试使用量角器测试元素是否可见.这是元素的样子:
<i class="icon-spinner icon-spin ng-hide" ng-show="saving"></i>
Run Code Online (Sandbox Code Playgroud)
在chrome控制台中,我可以使用这个jQuery选择器来测试元素是否可见:
$('[ng-show=saving].icon-spin')
[
<i class=?"icon-spinner icon-spin ng-hide" ng-show=?"saving">?</i>?
]
> $('[ng-show=saving].icon-spin:visible')
[]
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在量角器中执行相同操作时,我在运行时遇到此错误:
InvalidElementStateError:
invalid element state: Failed to execute 'querySelectorAll' on 'Document':
'[ng-show=saving].icon-spin:visible' is not a valid selector.
Run Code Online (Sandbox Code Playgroud)
为什么这无效?如何使用量角器检查可见性?
出于某种原因,当我在工作中运行我的测试时,浏览器被最大化,但是当我在家中运行它时,它只打开一个宽度约为50%的浏览器窗口.这会导致向下滚动等一些差异,所以我最好让它在运行测试的每台机器上打开一个相同大小的浏览器窗口.最好的方法是什么?(我已经找到了其他语言的一些答案,但还没能使它们适应javascript)
添加
browser.executeScript('window.moveTo(0,0);'+
'window.resizeTo(screen.width, screen.height);');
Run Code Online (Sandbox Code Playgroud)
什么都不做,(显然window.moveTo
并window.resizeTo
没有铬支持).
javascript webdriver angularjs selenium-webdriver protractor
如果Protractor正在取代Angular Scenario Runner进行E2E测试,这是否意味着我仍然可以将它与Karma一起用作我的E2E测试框架?
在量角器的文档中,我看到以下示例:
describe('by model', function() {
it('should find an element by text input model', function() {
var username = element(by.model('username'));
username.clear();
username.sendKeys('Jane Doe');
var name = element(by.binding('username'));
expect(name.getText()).toEqual('Jane Doe');
});
Run Code Online (Sandbox Code Playgroud)
这里显而易见的是你可以使用"by.model"在输入框中设置值,但是如果你想看一个输入框并看看里面有什么,你需要使用"by.binding".
我有一组代码(总结)我做的:
element(by.model('risk.name')).sendKeys('A value');
expect(element(by.model('risk.name')).getText()).toEqual('A value');
Run Code Online (Sandbox Code Playgroud)
(在我的实际代码中,我保存实体,然后在编辑模式下回到它,我正在检查我的值实际上已保存.但它仍然归结为同样的事情,这个示例代码给出了同样的问题).
这给了我一个错误:
Error: Expected '' to equal 'A value'.
Run Code Online (Sandbox Code Playgroud)
从理论上讲,按照文档中的示例,我可以做:
element(by.model('risk.name')).sendKeys('A value');
expect(element(by.binding('risk.name)).getText()).toEqual('A value');
Run Code Online (Sandbox Code Playgroud)
但是by.binding似乎不像完全限定的模型,我得到一个错误:
Error: No element found using locator: by.binding("risk.name")
Run Code Online (Sandbox Code Playgroud)
如果我这样做,它确实有效(经过时尚):
element(by.model('risk.name')).sendKeys('A value');
expect(element(by.binding('name')).getText()).toEqual('A value');
Run Code Online (Sandbox Code Playgroud)
这会找到一个元素,但也会发出警告,我有多个与'name'匹配的元素.不幸的是,它选择的那个不是正确的.
那么,有两个问题:
编辑:
我也尝试过vdrulerz建议的解决方案,我修改了代码如下:
element(by.model('risk.name')).getText().then(function(text) {
console.log(text);
expect(text).toEqual('A risk name');
});
Run Code Online (Sandbox Code Playgroud)
console.log返回一个空值(不是一个promise或一个对象),expect没有给出消息:
Expected '' to equal …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Protractor来测试Angular应用程序,并且还没有弄清楚如何检测元素是否具有特定的类.
在我的情况下,测试点击提交按钮,现在我想知道form [name ="getoffer"]是否具有类.ngDirty.可能是什么解决方案?
describe('Contact form', function() {
beforeEach(function(){
browser.get('http://localhost:9000');
element(by.linkText('Contact me')).click();
});
it('should fail form validation, all fields pristine', function() {
element(by.css('.form[name="getoffer"] input[type="submit"]')).click();
expect(element(by.name('getoffer'))).toHaveClass('ngDirty'); // <-- This line
});
});
Run Code Online (Sandbox Code Playgroud) 我想使用angularjs e2e测试来测试文件上传.你如何在e2e测试中做到这一点?我通过咕噜的业力来运行我的测试脚本.
protractor ×10
angularjs ×8
javascript ×5
testing ×3
selenium ×2
file ×1
file-upload ×1
gruntjs ×1
jasmine ×1
karma-runner ×1
python ×1
visible ×1
webdriver ×1