簡體   English   中英

selenium 使用 selenium javascript webdriver 測試失敗

[英]selenium test is failing using selenium javascript webdriver

我已經安裝了javascript selenium webdriver並編寫了這樣的第一個測試

我正在編寫登錄測試。 我輸入正確的用戶名和密碼並嘗試登錄。

describe('Checkout Google.com', function () {
let driver;
before(async function () {
    // driver = await new Builder().forBrowser('chrome').build();
});
it('Search on Google', async function () {
    let driver = await new Builder().forBrowser('chrome').build();
    await driver.get('http://alpdev.s3-website-us-east-1.amazonaws.com/');
    // await driver.findElement(By.name('q')).sendKeys('selenium', Key.RETURN);
    this.timeout(5000);
    await driver.findElement(By.xpath("//input[@placeholder='Email']")).sendKeys('owais@demoshop.com');
    await driver.findElement(By.xpath("//input[@placeholder='Password']")).sendKeys('test');
    await driver.findElement(By.className('btn-submit')).click().then(()=> done());
    // assert.equal(title, 'dalenguyen - Google Search');
   });
    // close the browser after running tests
    after(() => driver && driver.quit());
 })

而我的package json是這樣的

{
   "name": "selenium-01",
   "version": "1.0.0",
   "description": "",
   "main": "index.js",
   "scripts": {
     "test": "mocha --recursive index.js"
   },
   "author": "",
   "license": "ISC",
   "dependencies": {
     "mocha": "^7.2.0",
     "selenium-webdriver": "^4.0.0-alpha.7"
   }
}

現在,當我運行ng run test時,瀏覽器打開並通過視覺測試,這意味着登錄正在發生,但在控制台上打印如下

在此處輸入圖像描述

這里有什么問題,它給出了一個錯誤。

因此,我無法完全重現您的情況,因為腳本中的url不再可用,但發現了一些缺陷並嘗試了一些修復。

首先是你的driver被宣布了兩次。

其次,我認為主要問題(您收到上述錯誤的原因)是您使用的是then & done承諾,因為您使用的是async & await功能,所以不需要這些承諾。

另外,我建議您使用css定位器而不是xpaths

另一件事是您可以在after();中使用async 閉包,你可以使用箭頭函數(async () => {}); 到處而不是使用function關鍵字。

下面是您的示例,但有一些更改,我非常肯定它會起作用(盡管如果這一切都在谷歌搜索頁面上,那么該頁面上沒有輸入,因此這些步驟將失敗,您必須添加一些額外的使用可用輸入字段加載登錄頁面的步驟):

describe('Checkout Google.com', function () {
    let driver;
    before(async () => {
        driver = await new Builder().forBrowser('chrome').build();
    });

    it('Search on Google', async () => {
        await driver.get('http://alpdev.s3-website-us-east-1.amazonaws.com/');
        await driver.findElement(By.name('q')).sendKeys('selenium', Key.RETURN);
        await driver.sleep(5000);

        // the steps below will fail as per the description above
        await driver.findElement(By.css("input[placeholder='Email']")).sendKeys('owais@demoshop.com');
        await driver.findElement(By.css("input[placeholder='Password']")).sendKeys('test');
        await driver.findElement(By.className('btn-submit')).click();
        // assert.equal(title, 'dalenguyen - Google Search');
    });

    // close the browser after running tests
    after(async () => {
        await driver.quit();
    });
});

我希望這會有所幫助,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM