If the text of link/webElement is continuously changing then we call it as dynamic link or web element. We have two scenarios, first scenario is text of link may completely change second scenario is text of link may partially change. If it is partially changing we can use locator partailLinkText for links and contains function in xpath for other than link. If the text is completely changing we can go for other parameters of web element. Sometimes no parameter is unique in this case we can go for independent-dependent xpath.
Create specification file with name clickOnLink.js. Write the code to open google.com. Enter search text in search box. Click on search button. Find the relevant website in search results and click on it.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
describe('Click on the link', function() { it('Click on website link', function() { originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000; browser.waitForAngularEnabled(false); browser.get("www.google.com"); browser.manage().timeouts().implicitlyWait(10000); browser.driver.manage().window().maximize(); element(by.id('lst-ib')).sendKeys("Housing Society Software"); element(by.xpath("//input[@value='Google Search' and @type='submit']")).click(); element(by.xpath("//cite[contains(text(),'www.societyhive.com')]")).click(); }); }); |
- In “describe” block we need to mention test case description. In “it” block we need to mention test step description. We can have multiple “it” blocks inside one describe block. And we can have “describe” block inside “describe” block.
- In first two lines of code we are increasing “jasmine.DEFAULT_TIMEOUT_INTERVAL” because by default it is set to 10000 milliseconds. If complete test step is taking more than default it will fail with error “Error: Timeout – Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL”. To avoid this failure we should increase it.
- In third line of code we are making waitForAngularEnabled as false. Protractor can be used for both angular and non angular applications. If we using protractor for non angular web application we need to make it as false. If it is angular application we need to mention it as true. It helps to wait for angular web elements implicitly.
- To open the browser we need to use get(URL); function.
- We have defined implicit wait which will wait for all web elements.
- In sixth line we are maximizing the browser.
- In seventh line we are identifying the google search box using ID and entering value using sendKeys() function.
- And then identifying google search button using xpath and performing click operation on it.
- In last line identifying our website using xpath and clicking on it.
- If the URL is dynamic. For example it may display in formats “www.societyhive.com” or “https://www.societyhive.com/” then we can use contains method in xpath.
- If the web element is a link(having tag as ‘a’) and its text is partially changing then we can use locator “partialLinkText”.