Navigation and Browser (Part 1)
- Open URL and use some useful methods to verify page
- Define browser size and position: maximaze, minimaze
- Switch between browser windows or tabs
- Perform action "Refresh" and simulate "Back", "Forward"
1. Open URL and use some useful methods to verify page
baseUrl
url()
source()
getWebDriver().getTitle();
To visit page you can just specify url:
open("http://chromedriver.storage.googleapis.com/index.html?path=2.16");
But it is better to specify base url and use relative urls to visit pages:
url()
source()
getWebDriver().getTitle();
To visit page you can just specify url:
open("http://chromedriver.storage.googleapis.com/index.html?path=2.16");
But it is better to specify base url and use relative urls to visit pages:
baseUrl = "http://chromedriver.storage.googleapis.com";
open("/index.html?path=2.16");
To get current page url use url():
open("https://www.ukr.net/");
url(); // return "https://www.ukr.net/"
assertTrue(Html.text.contains(url(), "https://www.ukr.net/"));
To get current page source use source():
baseUrl = "http://chromedriver.storage.googleapis.com";
open("/index.html?path=2.16");
assertTrue(Html.text.containsCaseSensitive(source(), "Parent Directory"));
To get the current page title you need to come back to webdriver and use getWebDriver().getTitle():
open("https://www.ukr.net/");
getWebDriver().getTitle(); // return page title
assertEquals(getWebDriver().getTitle(), "UKR.NET: Всі новини України, останні новини дня в Україні та Світі");
2. Define browser size and position: maximaze, minimaze
Easy,
startMaximized = true; //to maximize on start
baseUrl = "http://www.seleniumhq.org";
open("/");
sleep(1000);
getWebDriver().manage().window().setSize(new Dimension(0,0)); // to minimize
sleep(1000);
getWebDriver().manage().window().maximize(); // to maximize
sleep(1000);
getWebDriver().manage().window().setPosition(new Point(100,100)); // to move
sleep(1000);
By the way. Avoid using "sleep()" in your tests. Better use implicitly/explicitly waits or be creative or just use Selenide. Yes, selenide understands when to wait (at least - in the most common situations, as said on the official site), for other cases waits methods are available to you.
Remember that "sleep pattern" is evil for your tests. I use sleep in this example to demonstrate changes in size and position.
3. Switch between browser windows or tabs
1 There is no difference between windows and tabs.
2. But there is difference for your browser settings
When I was preparing example I found that my chrome does not open links in new tabs/new windows. So I used custom chrome profile with extension Click to Tab to open links in new tabs.
Recipe for switching between windows or tabs is the same as for Selenium.
1. Open page#1
2. Click link to page#2
Result: New tab/window is opened
3. Get all opened tabs
Result: Number of handled tabs == 2
4. Switch to handled tab with index 1
Result: You can work with page#2
5. Switch to handled tab with index 0
Result: You can work with page#1
6. Switch to "default content"
Result: You can work with page#1
Switch to default content is NOT the same that switch handled tab with index 0.
Given I open page#1 and click link to page#2.
Result: New tab/window with page#2 is opened but I still work with page#1
Given I get all opened tabs
Result: Number of handled tabs == 2
Given I switch to tab with index 1
Result: You can work with page#2
Given I switch to default content
Result: You can work with page#2
Please see below an example with 3 tabs:
public class SwitchBetweenWindowsTabsTest
{
@Test
public void userCanSwitchBetweenTabs()
{
baseUrl = "http://www.seleniumhq.org";
open("/");
System.out.println("Open, stay on page: " + getWebDriver().getTitle());
assertEquals(getWebDriver().getTitle(), "Selenium - Web Browser Automation");
$("#menu_projects").click();
System.out.println("Click '#menu_projects', stay on page: " + getWebDriver().getTitle());
$("#menu_download").click();
System.out.println("Click '#menu_download', stay on page: " + getWebDriver().getTitle());
// Array<String> realization
ArrayList<String> tabs = new ArrayList<String> (getWebDriver().getWindowHandles());
System.out.println("Tabs count = " + tabs.size());
getWebDriver().switchTo().window(tabs.get(1));
System.out.println("After get(1) switch, stay on page: " + getWebDriver().getTitle());
assertEquals("Downloads", getWebDriver().getTitle());
getWebDriver().switchTo().window(tabs.get(2));
System.out.println("After get(2) switch, stay on page: " + getWebDriver().getTitle());
assertEquals("Selenium Projects", getWebDriver().getTitle());
getWebDriver().switchTo().defaultContent();
System.out.println("After defaultContent() switch, stay on page: " + getWebDriver().getTitle());
assertEquals("Selenium Projects", getWebDriver().getTitle());
getWebDriver().switchTo().window(tabs.get(0));
System.out.println("After get(0) switch, stay on page: " + getWebDriver().getTitle());
assertEquals("Selenium - Web Browser Automation", getWebDriver().getTitle());
getWebDriver().switchTo().window(tabs.get(2));
System.out.println("Once again get(2) switch, stay on page: " + getWebDriver().getTitle());
assertEquals("Selenium Projects", getWebDriver().getTitle());
}
}
By the way try to avoid System.out.println() in your real tests. Use normal logging.
4. Perform action "Refresh" and simulate "Back", "Forward"
// refresh current page
refresh();
To simulate "back" and "forward" you can store urls and use open() in necessary order .
public class RefreshBackForwardTest
{
@Test
public void userCanUseRefreshBackForward()
{
baseUrl = "http://www.seleniumhq.org";
open("/");
String defaultUrl = url();
assertEquals("Selenium - Web Browser Automation", getWebDriver().getTitle());
String projectstUrl = $("#menu_projects a").getAttribute("href");
String downloadUrl = $("#menu_download a").getAttribute("href");
open(projectstUrl);
assertEquals("Selenium Projects", getWebDriver().getTitle());
open(downloadUrl);
assertEquals("Downloads", getWebDriver().getTitle());
// Simulate "Back"
open(projectstUrl);
assertEquals("Selenium Projects", getWebDriver().getTitle());
open(defaultUrl);
assertEquals(getWebDriver().getTitle(), "Selenium - Web Browser Automation");
// Simulate "Forward"
open(projectstUrl);
assertEquals("Selenium Projects", getWebDriver().getTitle());
open(downloadUrl);
assertEquals("Downloads", getWebDriver().getTitle());
// And refresh current page
refresh();
}
}
No comments:
Post a Comment