如何使用JavaScript使“ dragAndDrop”与Selenium一起使用?

时间:2019-04-25 17:03:02

标签: javascript selenium-webdriver drag-and-drop selenium-chromedriver

我正在为我正在处理的网页创建Selenium测试,但尝试使用dragAndDrop操作时遇到麻烦。我需要使用此操作来重新排序我的等待列表。我是Selenium的新手,我不确定dragAndDrop操作是否使用了正确的语法。如果有人可以帮助我朝正确的方向前进,我将不胜感激。下面是我当前的代码减去我编写的其他测试。

const webdriver = require('selenium-webdriver');
const keys = webdriver.Key;
const chromedriver = require('chromedriver');
const chrome = require('selenium-webdriver/chrome');
const rp = require('request-promise');
const chai = require('chai');
const assert = chai.assert;
const expect = chai.expect;
const should = chai.should();
const WebElement = require('selenium-webdriver');
const actions = require('selenium-webdriver/lib/input');

driver = new webdriver.Builder().forBrowser('chrome').build();

describe('This is for testing the Rewards Portal using Selenium', function() {
this.timeout(60000);

  it('Should reorder the customers', async function () {
        let drag = driver.findElement(webdriver.By.xpath('//*[@id="LiveScheduleList"]/ul/li[1]/div[2]'));
        let drop = driver.findElement(webdriver.By.xpath('//*[@id="LiveScheduleList"]/ul/li[2]/div[2]'));

        await driver.sleep(2500);

        await driver.executeScript("document.querySelector('#ChangeOrderBtn').click();");

        await driver.sleep(2500);

        await actions.dragAndDrop(drag, drop);

    });
};

当我运行它时,我会得到以下信息: TypeError: actions.dragAndDrop is not a function

1 个答案:

答案 0 :(得分:0)

您使用的操作不正确。

您的代码应类似于...

await driver.
      actions().
      dragAndDrop(drag, drop);

PD:顺便说一句,使用睡眠来同步代码不是最佳做法...