找到不可见的值

时间:2016-02-01 17:11:38

标签: java selenium-webdriver webdriver pageobjects

我有问题我需要比较货币(左上角),它设置为我选择的货币:

<div class="btn-group">
    <button class="btn btn-link dropdown-toggle" data-toggle="dropdown">
        <strong>£</strong>
        <span class="hidden-xs hidden-sm hidden-md">Currency</span> <i class="fa fa-caret-down"></i>
    </button>
    <ul class="dropdown-menu">
        <li><button class="currency-select btn btn-link btn-block" type="button" name="EUR">€ Euro</button></li>
        <li><button class="currency-select btn btn-link btn-block" type="button" name="GBP">£ Pound Sterling</button></li>
        <li><button class="currency-select btn btn-link btn-block" type="button" name="USD">$ US Dollar</button></li>
    </ul>
</div>

我在这里选择货币:

public void setCurrency(String currencyToSet){
    WebElement currencyVal= driver.findElement(By.name(currencyToSet.toUpperCase()));
    currencyVal.click();    
}

我不知道如何比较我选择的货币与设定的货币。

它的外观如下:demo.opencart.com

2 个答案:

答案 0 :(得分:1)

我的假设是 - 您首先想要设置货币说'欧元',然后您想通过验证货币符号来检查是否设置了'欧元'。

那么,你能做的是 - 1.在选择货币名称之前获取货币名称,将其存储在变量中。 2.选择货币 3.最后,通过按空格分割,从货币名称中提取货币符号。 4.现在,由于您有货币符号,您可以轻松地将其与设定值进行比较。

这是C#代码

[Test]
    public void Test3()
    {
        string currencyToSet = "EUR";

        IWebElement currencyDropDown = driver.FindElement(By.CssSelector("button[class='btn btn-link dropdown-toggle']"));
        currencyDropDown.Click();

        //Locate the Currency
        IWebElement currencyVal = driver.FindElement(By.Name(currencyToSet.ToUpper()));

        //Before clicking Currency, store Currency Name in a variable.
        string currencyName = currencyVal.Text;

        //Print Currency Name
        Console.WriteLine("Currency Name:" + currencyName);

        //Click on Currency
        currencyVal.Click();

        //Extract Currency Symbol from Currency Name
        string[] spittedCurrencyName = currencyName.Split(' ');
        string currencySymbol = spittedCurrencyName[0];

        //Now you have Currency Symbol. Therefore, you can easily compare it with what is set. 
        Console.WriteLine("Currency Symbole: " + currencySymbol);
    }

我希望这会有所帮助:)

答案 1 :(得分:1)

这对我有用。希望它会对你有所帮助。

driver.get("http://demo.opencart.com/");

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

driver.findElement(By.xpath("//button[@class='btn btn-link dropdown-toggle']")).click();

//WebElement selectCurrency = driver.findElement(By.xpath("//button[@name='USD']"));
//WebElement selectCurrency = driver.findElement(By.xpath("//button[@name='GBP']"));
WebElement selectCurrency = driver.findElement(By.xpath("//button[@name='EUR']"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", selectCurrency);

String[] currency = driver.findElement(By.xpath("//div[@id='cart']//span")).getText().split("- ");

String[] c1 = currency[1].split("0.00");

System.out.println(currency[1]);
System.out.println(c1[0]);

if(c1[0].equalsIgnoreCase("$"))
{
    System.out.println("Doller selected");
}
else if(c1[0].equalsIgnoreCase("£"))
{
    System.out.println("Pound");
}
else if(c1[1].equalsIgnoreCase("€"))
{
    System.out.println("Euro");
}