You might have come across some scenarios in automating a web application wherein clicking a hyperlink object opens the new page in a new tab of the browser. You can not perform any operation on the objects of the newly opened tab unless you attach them first. In this tutorial, we will learn how to access and automate a new tab in LeanFT. We will use BrowserFactory class to attach the newly opened browser tab so that we can automate the new tab objects.
Example-Automate a new tab in LeanFT | UFT Developer
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 41 42 43 44 45 46 47 48 49 50 51 |
import org.testng.annotations.Test; import com.hp.lft.report.ReportException; import com.hp.lft.report.Reporter; import com.hp.lft.report.Status; import com.hp.lft.sdk.GeneralLeanFtException; import com.hp.lft.sdk.web.Browser; import com.hp.lft.sdk.web.BrowserDescription; import com.hp.lft.sdk.web.BrowserFactory; import com.hp.lft.sdk.web.BrowserType; import com.hp.lft.sdk.web.Link; import com.hp.lft.sdk.web.LinkDescription; import com.hp.lft.sdk.web.WebElement; import com.hp.lft.sdk.web.WebElementDescription; import com.hp.lft.verifications.Verify; import unittesting.UnitTestClassBase; public class NewTab extends UnitTestClassBase { @Test public void testMerury() throws GeneralLeanFtException, ReportException { try { Browser browser = BrowserFactory.launch(BrowserType.CHROME); //Navigate to the New Tours website. browser.navigate("http://newtours.demoaut.com/"); // Click the "Cruises" link (on the left side of the page). browser.describe(Link.class, new LinkDescription.Builder() .tagName("A").innerText("Cruises").build()).click(); //Attach the new (replacement) browser tab. Browser TabObj = BrowserFactory.attach(new BrowserDescription.Builder().title("Cruises: Mercury Tours").build()); //Create object of a web element in the newly opened tab WebElement nightsFrom850WebElement = TabObj.describe(WebElement.class, new WebElementDescription.Builder() .innerText("7 nights from $850") .tagName("FONT").build()); //Validate that the text '7 nights from $850' exists. Verify.isTrue(nightsFrom850WebElement.exists(), "Verify_nightsFrom850WebElement", "Validate that the text '7 nights from $850' exists."); } catch(Error e){ Reporter.reportEvent("Runtime error occured","Failed during test",Status.Failed, e); throw e; } } } |
Code Explanation-New Tab Automation
You just simply can’t identify the objects of a web page that has opened in a new tab after clicking any hyperlink in the current object. To handle new tab objects, you will have to first attach the newly opened tab to let LeanFT identify its object. You can attach the newly opened tab using its title. The same has been done using BrowserFactory in the above code and the returned value has been stored in the Browser object type variable(TabObj)
1 2 3 4 5 |
//Attach the new (replacement) browser tab. Browser TabObj = BrowserFactory.attach(new BrowserDescription.Builder().title("Cruises: Mercury Tours").build()); |
Now using this TabObj you can access all the elements of the newly opened tab and perform the desired action.