Basic Methods of WebDriver

package ui; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.safari.SafariDriver; import static org.openqa.selenium.support.locators.RelativeLocator.with; import io.github.bonigarcia.wdm.WebDriverManager; public class DemoAutoClass { public static String browser="Chrome"; public static WebDriver driver; public static void main(String[] args) { // TODO Auto-generated method stub if(browser.equals("Firefox")) { WebDriverManager.firefoxdriver().setup(); driver = new FirefoxDriver(); } else if(browser.equals("Chrome")){ WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); } else if(browser.equals("Safari")){ WebDriverManager.safaridriver().setup(); driver = new SafariDriver(); } else if(browser.equals("Edge")){ WebDriverManager.edgedriver().setup(); driver = new EdgeDriver(); } driver.get("https://www.saucedemo.com/v1/"); driver.manage().window().maximize(); String currentUrl =driver.getCurrentUrl(); System.out.println(currentUrl); String title =driver.getTitle(); System.out.println(title); String pagesource =driver.getPageSource(); System.out.println(pagesource); driver.navigate().to("https://www.google.com/"); driver.close();get (String url):
- Purpose: Loads a new web page in the current browser window.
- Usage in Code: driver.get ("https: //www .saucedemo .com/v1/ ");
- Description: This method is used to navigate to a specified URL. It waits for the page to load completely before proceeding to the next command.
manage():
- Purpose: Provides access to browser's management interface.
- Usage in Code: driver .manage() .window() .maximize();
- Description: This method returns an Options interface which provides methods to manage different browser settings such as cookies, timeouts, and window sizes. In this example, it is used to maximize the browser window.
getCurrentUrl():
- Purpose: Retrieves the URL of the current page.
- Usage in Code: String currentUrl = driver .getCurrentUrl();
- Description: This method returns a string representing the current URL the browser is viewing.
getTitle():
- Purpose: Retrieves the title of the current page.
- Usage in Code: String title = driver .getTitle();
- Description: This method returns the title of the current web page as a string.
getPageSource():
- Purpose: Retrieves the source code of the current page.
- Usage in Code: String pagesource = driver .getPage Source();
- Description: This method returns the HTML source code of the current web page as a string.
navigate():
- Purpose: Provides methods to navigate the browser.
- Usage in Code: driver .navigate() .to("https: //www.google.com/");
- Description:
- navigate() .to (String url): Navigates to the specified URL.
- navigate() .back(): Goes back to the previous page in the browser's history.
- navigate() .forward(): Moves forward to the next page in the browser's history
- navigate() .refresh(): Reloads the current page.
close():
- Purpose: Closes the current browser window.
- Usage in Code: driver .close();
- Description: This method closes the current browser window and all its tabs