Keyword driven framework

In this example, we set the path to the chromedriver executable using the System.setProperty() method, create a new instance of the Chrome driver, navigate to a webpage, find an element by its ID using the findElement() method with a By object, and then click on it using the click() method of the WebElement class. Finally, we close the browser using the quit() method of the driver.

Note that you will need to have the appropriate Selenium WebDriver installed for the browser you want to automate, and you will need to add the appropriate import statements at the beginning of your Java file.

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
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.firefox.FirefoxDriver;

public class KeywordDrivenFramework {
public static void main(String[] args) throws IOException {
// Load the properties file
FileInputStream file = new FileInputStream(“path/to/keywords.properties”);
Properties prop = new Properties();
prop.load(file);

// Get the browser and URL from the properties file
String browser = prop.getProperty(“browser”);
String url = prop.getProperty(“url”);

// Create a new instance of the driver based on the browser specified
WebDriver driver = null;
if (browser.equalsIgnoreCase(“chrome”)) {
System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase(“firefox”)) {
System.setProperty(“webdriver.gecko.driver”, “path/to/geckodriver”);
driver = new FirefoxDriver();
}

// Navigate to the URL specified in the properties file
driver.get(url);

// Loop through the keywords in the properties file
for (int i = 1; i <= prop.size(); i++) {
String keyword = prop.getProperty(“keyword” + i);

// Execute the appropriate action based on the keyword
if (keyword.startsWith(“open”)) {
String locator = keyword.split(“:”)[1];
driver.findElement(By.id(locator)).click();
} else if (keyword.startsWith(“type”)) {
String[] locatorAndValue = keyword.split(“:”)[1].split(“,”);
String locator = locatorAndValue[0];
String value = locatorAndValue[1];
WebElement element = driver.findElement(By.id(locator));
element.clear();
element.sendKeys(value);
} else if (keyword.startsWith(“click”)) {
String locator = keyword.split(“:”)[1];
driver.findElement(By.xpath(locator)).click();
} else if (keyword.startsWith(“wait”)) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

// Close the browser
driver.quit();
}

}