Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在typescript中返回动态XPath数组?_Typescript_Protractor - Fatal编程技术网

如何在typescript中返回动态XPath数组?

如何在typescript中返回动态XPath数组?,typescript,protractor,Typescript,Protractor,我有一组XPath,它们需要参数来选择适当的元素。然而,下面的方法似乎不起作用 async returnSection(sectionName: string, childItem: string): Promise<WebElement> { let selectedSection = element(by.xpath(`//div[@class='default-name info' and text()='${sectionName}']`));

我有一组XPath,它们需要参数来选择适当的元素。然而,下面的方法似乎不起作用

async returnSection(sectionName: string, childItem: string): Promise<WebElement> {

        let selectedSection = element(by.xpath(`//div[@class='default-name info' and text()='${sectionName}']`));
        let aChildItem = element(by.xpath(`//div[@class='default-name' and text()='${childItem}']`));
        let customName = element(by.xpath(`//div[@class='default-name' and text()='${childItem}']/../../../..//input[@placeholder='None']`));
        let pageObjects: WebElement[] = [selectedSection, aChildItem, customName];
        // return pageObjects[] //I am not able to return this array.

    }
还是有更好的方法来处理这种情况

//return pageObjects[]//我无法返回此数组

您将返回类型注释为Promise,但希望返回一个WebElement数组

修理 改变

async returnSection(sectionName: string, childItem: string): Promise<WebElement> {


此解决方案可以工作,但如果处理不当,将出现许多未定义的问题。此外,使用这种方法,在VSCode上进行调试会容易得多

要处理这样的情况,必须改变方法。与其返回WebElements数组,不如像这样返回XPath字符串

    useBenefits(sectionName: string, childItemName: string): string[] {
        let benefitSection = `//div[@class='default-name info' and text()='${sectionName}']`;
        let childItem = `//div[@class='default-name' and text()='${childItemName}']`;
        let customText = `//div[@class='default-name' and text()='${childItem}']/../../../..//input[@placeholder='None']`;
        return new Array(benefitSection, childItem, customText);
    }
现在通过传递参数并将其存储在数组中来调用此函数。将数组元素用作WebElement的XPath


    let abc1: string[] = this.expertPIAHelper.useSectionBenefits('Achievement', "Confidence");
        let section = element(by.xpath(abc1[0]));
        let child = element(by.xpath(abc1[1]));
        let custom = element(by.xpath(abc1[2]));
        await this.helper.click(section);
        await browser.sleep(1000);
        await this.helper.enterText(custom, "Custom Name");
        await browser.sleep(1000);
        await this.helper.click(this.selectedBenefitSection);
        await browser.sleep(1000);


使代码维护和调试更加容易。

谢谢您的回答。它奏效了:但我找到了更好的方法来处理我的处境。可能对其他遇到类似问题的人有用。
    useBenefits(sectionName: string, childItemName: string): string[] {
        let benefitSection = `//div[@class='default-name info' and text()='${sectionName}']`;
        let childItem = `//div[@class='default-name' and text()='${childItemName}']`;
        let customText = `//div[@class='default-name' and text()='${childItem}']/../../../..//input[@placeholder='None']`;
        return new Array(benefitSection, childItem, customText);
    }

    let abc1: string[] = this.expertPIAHelper.useSectionBenefits('Achievement', "Confidence");
        let section = element(by.xpath(abc1[0]));
        let child = element(by.xpath(abc1[1]));
        let custom = element(by.xpath(abc1[2]));
        await this.helper.click(section);
        await browser.sleep(1000);
        await this.helper.enterText(custom, "Custom Name");
        await browser.sleep(1000);
        await this.helper.click(this.selectedBenefitSection);
        await browser.sleep(1000);