How to Open Browser in Selenide

Selenide supports only web based application, opening a browser for any operation is must. We cannot access already opened browser in selenide.

Opening a browser in selenide is so simple, we donot even have to create any object. What we have to do is just give a call to open() method and pass the url.

We have to be careful about the import statement, your IDE might not support automatic imports, Firefox is the default browser

Be aware that selenide throws org.openqa.selenium.InvalidArgumentException if we pass the url without protocol like ftp,http,https


Let's open Selenide site : https://selenium-webdriver.com

                                import static com.codeborne.selenide.Selenide.*;
                                import static com.codeborne.selenide.Condition.*;
                                import static com.codeborne.selenide.WebDriverRunner.*;
                                
                                public class SelenideOpenBrowser {
                                    public static void main(String[] args) {
                                        // open firefox browser.
                                        open("https://google.com");
                                    }
                                }
                                

Open Chrome browser in Selenide

By default Selenide opens the Firefox, to open any other browser we have to explicitly mention the driver executable path to System property.

And set the selenide.browser to chrome, after setting the executable and browser name call open() method with parameter.


                            import static com.codeborne.selenide.Selenide.open;
                            
                            public class OpenChrome {
                                
                                public static void main(String[] args) {
                                    // set the chromedriver.exe to system
                                    System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Pictures\\chromedriver.exe");
                                    // set the browser name to chrome
                                    System.setProperty("selenide.browser", "Chrome");
                                    open("https://selenium-webdriver.com");
                                }
                            }
                            

Open Internet Explorer in Selenide

We can open IE browser with selenide, to open IE browser we have to set the IEdriverserver.exe and the browser name to Internet Explorer


                            import static com.codeborne.selenide.Selenide.open;
                            
                            public class OpenIE {
                                
                                public static void main(String[] args) {
                                    // set the IEdriverserver.exe to system
                                    System.setProperty("webdriver.ie.driver", "C:\\Users\\user\\Pictures\\IEdriverserver.exe");
                                    // set the browser name to IE
                                    System.setProperty("selenide.browser", "Internet Explorer");
                                    open("https://selenium-webdriver.com");
                                }
                            }
                                                        

Close browser in Selenide

We donot have to close the browser as the selenide takes care of the closing browser after the test execution. But If you want close before execution you can use close() method


                            import static com.codeborne.selenide.Selenide.open;
                            
                            public class WorkOffline {
                                public static void main(String[] args) {
                                    // open the oofline webpage
                                    open("https://selenium-webdriver.com");
									close();
                                }
                            }
                            

Work offline in Selenide

Sometime we may not have internet but we may be in practice phase, in those kind of scenarios we can use selenide to work with offline web application.

Note: If the site doesnot have the the protocol then just copy paste the url into eclipse it will automatically imports the protocol.


                            import static com.codeborne.selenide.Selenide.open;
                            
                            public class WorkOffline {
                                public static void main(String[] args) {
                                    // open the oofline webpage
                                    open("file:///C:/Users/user/Music/Site/java/work-offline.php");
									close();
                                }
                            }
                            

Get Title in Selenide

We can get title of webpage using title() method in selenide, below image shows where the page title present in the source code title-selenide


                            import static com.codeborne.selenide.Selenide.*;
                            
                            public class GetTitle {
                                public static void main(String[] args) {
                                    // open url page
                                    open("https://selenium-webdriver.com");
                                    System.out.println("Title of the Page is : "+title());
                                }
                            }
                            

output-title-selenide

Get Url Of the page in Selenide

We can get the url of the page using url() method.


                                import static com.codeborne.selenide.Selenide.*;
                                import static com.codeborne.selenide.WebDriverRunner.*;
                                
                                public class GetUrl {
                                    public static void main(String[] args) {
                                        // open url page
                                        open("https://selenium-webdriver.com");
                                        System.out.println("URL of the Page is : " +url());
                                    }
                                }
                                

Get source code Of the page in Selenide

We can get the source code of the page using source() method.


                                import static com.codeborne.selenide.Selenide.*;
                                import static com.codeborne.selenide.WebDriverRunner.*;
                                
                                public class GetUrl {
                                    public static void main(String[] args) {
                                        // open page
                                        open("https://selenium-webdriver.com");
                                        System.out.println("Source code of the Page is : " +source());
                                    }
                                }
                                

Navigation commands are some which enables the user to navigate to through history like back, forth, refresh.

Below are the few Commands:
1. refresh()
2. back()
3. forward()

1. refresh() : refresh() method refreshes the current page


                                import static com.codeborne.selenide.Selenide.*;
                                
                                public class RefreshBrowser {
                                    public static void main(String[] args) {
                                        // open url page
                                        open("https://selenium-webdriver.com");
                                        // refresh the current page
                                        refresh();
                                    }
                                }
                                


2. back() : back method helps the user to navigate back.


                                import static com.codeborne.selenide.Selenide.*;
                                
                                public class RefreshBack {
                                    public static void main(String[] args) {
                                        // open url page
                                        open("https://selenium-webdriver.com");
                                        // navigate back
                                        back();
                                    }
                                }
                                


In above program you might not face any error but it also will not navigate anywhere ,why ? because back() calls the back() native method present in the browser indirectly so it works only when you navigate more than one page.Then only the button on the browser will be enabled unless it enabled the back() method never works.


Modify the above program like below one


                                import static com.codeborne.selenide.Selenide.*;
                                import static com.codeborne.selenide.WebDriverRunner.*;
                                
                                public class NavigateBack {
                                    public static void main(String[] args) {
                                        // open url page
                                        open("https://selenium-webdriver.com");
                                        // print url
                                        System.out.println(url());
                                        // navigate back
                                        open("https://google.com");
                                        // print url
                                        System.out.println(url());
                                        // navigate back
                                        back();
                                        // print url
                                        System.out.println(url());
                                    }
                                }
                                
navigate-back-selenide

Important Note : Notice that when we used open() method for second time, selenide doesnot open new browser, it just naviagtes to new url.


3. Forward() : The forward() method navigates the user to forth but if we want forward() method to work we should use back() before using forward() then only the forward button in browser is enabled


                                import static com.codeborne.selenide.Selenide.*;
                                import static com.codeborne.selenide.WebDriverRunner.url;
                                
                                public class BrowserForward {
                                    public static void main(String[] args) {
                                        // open url page
                                        open("https://selenium-webdriver.com");
                                        // print url
                                        System.out.println("Current url");
                                        System.out.println(url());
                                        
                                        System.out.println("Navigating to google");
                                        open("https://google.com");
                                        // print url
                                        System.out.println(url());
                                        
                                        System.out.println("Navigateing to back()");
                                        // navigate back
                                        back();
                                        // print url
                                        System.out.println(url());
                                        
                                        System.out.println("Navigateing to Forward");
                                        // navigate forward
                                        forward();
                                        System.out.println(url());
                                    }
                                }
                                
navigate-forward-selenide

You can also share knowledge by Adding a topic here



Comment / Suggestion Section

Point our Mistakes and Post Your Suggestions