Skip to main content
 首页 » 编程设计

C# If Else 语句选择合适的 Xpath 表达式

2024年02月13日14tintown

我正在使用 C#、Ranorex dll 库和 Xpath 表达式作为定位器编写自动化测试。这一切都很好,但在某些情况下,我想使用 If Else 语句从可能的 2 个 xpath 中选择合适的 Xpath:

Public class Locators 
{ 
    public const string Xpathone = "//form[@controlname='x1']"; 
    public const string Xpathtwo = "//form[@controlname='x2']"; 
} 
 
 
Step Definition 
[Binding] 
public class HomePageSteps 
{ 
    public void SelectAppropriateXpath() 
    { 
        Validate.Exists(HomePage.Header, Config.ExistsTimeOut); 
        if (Validate.Exists(HomePage.Xpathone))==true 
        { 
            mouse.click(HomePage.Xpathone); 
        } 
        else 
        { 
            mouse.click(HomePage.Xpathtwo); 
        } 
    } 

但是我继续得到一个错误:

Cannot implicitly convert type 'void' to 'bool'

即使我将类型更改为 bool:

public bool SelectAppropriateXpath()

错误仍然存​​在,这让我感到困惑。当我使用“public bool”时,我还会得到:

Error - not all code paths return a value

任何想法将不胜感激?

请您参考如下方法:

乍一看你的代码有错误:

if (Validate.Exists(HomePage.Xpathone))==true 

应该是:

if (Validate.Exists(HomePage.Xpathone))