简体   繁体   English

每次调用变量时如何调用方法

[英]How to call a method every time a variable is called

So I have a variable that uses a method. 所以我有一个使用方法的变量。 Something like this: 像这样:

By locator = By.id("something-"+getDynamicId());

Usually the id changes numbers like this: something-1 to something-2 (not those exact numbers, always different). 通常,id会更改数字,例如:something-1到something-2(不是确切的数字,总是不同的)。 I have a way of getting the number it changes to by calling the getDynamicId() method. 我有一种方法,可以通过调用getDynamicId()方法来获取更改为的数字。 The problem is, when I start my test the id is set at the start and whenever I click on a specific button, the id changes but my variable does not. 问题是,当我开始测试时,id会在开始时设置,并且每当我单击特定按钮时,id都会更改,但变量不会更改。 Is it possible for the locator variable to call getDynamicId() everytime locator is called? 是否有可能为定位变量调用getDynamicId()每次定位器叫什么名字? Maybe everytime I click on the specific button the locator reloads? 也许每次我单击定位器重新加载的特定按钮时?

I have looked up ClassLoaders but I do not know how to use it nor do I know if it can even do what I want. 我查找了ClassLoaders,但我不知道如何使用它,也不知道它是否可以满足我的要求。

Instead of doing that, i've gotten around dynamic ID's by using CSS. 除了这样做,我还通过使用CSS解决了动态ID。

driver.findElement(By.cssSelector("element[id^='something']"));

This means "find an <element> tag, that has an id attribute that starts with "something" 这意味着“找到一个<element>标记,该标记的id属性starts with ” something” starts with

If there are multiple elements that match this, then you need to increase your specificity. 如果有多个与此匹配的元素,则需要提高特异性。 You can do this by selecting some parent. 您可以通过选择一些父母来做到这一点。

div.some-div [id^='something']

            ^  the space here meaning "a descendent of.."

this page may help with formulating CSS selectors like this for Selenium. 此页面可能有助于为Selenium制定这样的CSS选择器。

Also, in addition to what @sircapsalot has done, you can use xpath for working with dynamic ids as below (As you said in comment, there are multiple element having ids that contains "something"): 另外,除了@sircapsalot所做的工作之外,还可以使用xpath如下处理动态ID(如您在评论中所述,有多个元素的ID包含“某物”):

List<WebElement> elements = driver.findElements(By.xpath("//tag[contains(@id,'something-')]"));
//Say you want to click on 4th element now then use the below code(assuming the list has more than or equal to 4 elements in it)
elements.get(3).click();

Here, it will locate all the elements with tag as "tag" and has "id" that contains the text "something-" and then click on the 4th element in the list. 在这里,它将找到所有标记为“ tag”的元素,并具有包含文本“ something-”的 “ id” ,然后单击列表中的第4个元素。


But, if you want to retrieve the dynamic ID and use it for clicking on the specific element, you can create a new method like this : 但是,如果要检索动态ID并将其用于单击特定元素,则可以创建一个新方法,如下所示:

public static void clickOnDynamicElementById(String partial_locator_id){

    //Code for getting dynamic id and then storing the retrieved dynamic ID in a string, say "dynaID";

    String locator_id_to_click = partial_locator_id + dynaID;

    driver.findElement(By.id("locator_id_to_click")).click();

}

Then, you can call this method directly in your main method as below: 然后,您可以直接在您的main方法中调用此方法,如下所示:

  clickOnDynamicElement("something-"); //because main method is also static, it can call the method directly.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM