As we stated in the previous article, in most cases it is impossible to make search in the admin panel using the ID parameter, so instead we have to frequently use the Xpath locators to effectively use the automation. But in this case along with losing the flexibility we also encounter another problem – it significantly increases the time to search and find an element and, therefore, the time to execute tests. This is not so noticeable if you are trying to automate acceptance tests only. But if you need to automate the complete project, the speed of the test execution increases enormously.
A possible solution would be to use the standard method Selenium – аssignId
1 2 |
public void assignId(java.lang.String locator, java.lang.String identifier) |
It temporarily assigns a specified identifier to an element. So later, before reloading the page, you can access this element directly through this identifier, not through the slow and cumbersome Xpath locator.
This method can be useful, for example, when elements are added on the page via аjаx without reloading the page. As seen on the picture below, elements are added via the Add Exception button without reloading the page and the element has a generated Id, which makes us to use the Xpath locator.
Therefore, we need to access the buttons “Delete” and “Add Exception” through the assigned identifier.
Here is an example of a test, which uses such method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
public class Test1 extends SeleneseTestCase { public void setUp() throws Exception { setUp("http://belvg.info/", "*firefox"); } public void runTest() throws Exception { selenium.open("/demo/magento/test/admin"); selenium.type("id=username","belvg_msh"); selenium.type("id=login","Ij8yEDwPCE"); selenium.click("css=input.form-button"); selenium.waitForPageToLoad("30000"); selenium.open("demo/magento/test/index.php/admin/system_config/edit/" + "section/design/"); selenium.waitForPageToLoad("30000"); selenium.click("id=design_package-head"); for (int second = 0;; second++) { if (second >= 60) { fail("timeout"); } try { if (selenium.isTextPresent("Add Exception")) { break; } } catch (Exception e) { } Thread.sleep(1000); } selenium.assignId("xpath=html/body/div[1]/div[3]/div/div/div[2]/" + "div/form/div[2]/div[1]/fieldset/table/tbody/tr[2]/" + "td[2]/div[2]/button", "btn"); selenium.click("btn"); } } |
It is also necessary to use the аssignId method in case you expect a page element with a generated Id to appear. This happens due to the fact that cycle is repeatedly searching for one and the same element, thus, before starting the cycle, assign first the identifier to an element via the method аssignId, and then access it directly in the cycle. This is how it looks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public void runTest() throws Exception { selenium.assignId("xpath=html/body/div[1]/div[3]/div/div/div[2]/div/form/div[2]/" + "div[1]/fieldset/table/tbody/tr[2]/td[2]/div[2]/button", "btn"); for (int second = 0;; second++) { if (second >= 60) { fail("timeout"); } try { if (selenium.isElementPresent("btn")) { break; } } catch (Exception e) { } Thread.sleep(1000); } } |
Another time-consuming procedure during the test execution is filling of the multiselect tag. It is also possible to speed it up using the аssignId method.
1 2 3 4 5 6 7 8 9 |
public void runTest() throws Exception { selenium.assignId("xpath=html/body/div[1]/form[3] ","multiselect"); selenium.select("multiselect", "value=1"); for (int i = 2; i < 99; i++) { selenium.addSelection("multiselect", "value=" + i); } } |
These are just some examples of possible assigning an Id to a page element. Use the аssignId method where it is necessary and you’ll see significant advances in your test performance optimization.
Looking for a reliable Magento support partner? Turn to BelVG!