Refined user field for configuring driver, testlogin should now work

This commit is contained in:
Xander 2025-04-02 17:39:34 +01:00
parent a60abd6fc3
commit 58aca1e828
3 changed files with 41 additions and 19 deletions

0
filename.txt Normal file
View file

View file

@ -1,6 +1,7 @@
package main; package main;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxOptions;
import java.io.IOException; import java.io.IOException;
@ -8,17 +9,18 @@ import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
public class ConfigureDriver { public class ConfigureDriver {
public static WebDriver ConfigureDriver() { public static WebDriver configureDriver() {
WebDriver driver; WebDriver driver;
String filePath = ".user"; // Ensure the file exists in the working directory String filePath = ".git/user"; // Ensure the file exists in the working directory
String user = ""; String user = "";
System.out.println("YOU MUST HAVE YOUR NAME PUT IN A FILE! In this repo's route, make .git/user (no extension) contain only your name.");
try { try {
user = Files.readString(Paths.get(filePath)).trim();; user = Files.readString(Paths.get(filePath)).trim();;
System.out.println("User read: " + user); System.out.println("User read: " + user + " in file");
} catch (IOException e) { } catch (IOException e) {
System.err.println("Error reading user file: " + e.getMessage()); System.err.println("Error reading user file: " + e.getMessage());
} }
if (user == "xander") { if (user.equals("xander")) {
System.setProperty("webdriver.gecko.driver", "/usr/local/bin/geckodriver"); System.setProperty("webdriver.gecko.driver", "/usr/local/bin/geckodriver");
FirefoxOptions options = new FirefoxOptions(); FirefoxOptions options = new FirefoxOptions();
options.setBinary("/usr/bin/firefox"); // Explicitly set Firefox binary options.setBinary("/usr/bin/firefox"); // Explicitly set Firefox binary
@ -26,7 +28,10 @@ public class ConfigureDriver {
} }
else { else {
/// DREW ADD YOUR WEB DRIVER CODE PLEASE /// DREW ADD YOUR WEB DRIVER CODE PLEASE
System.setProperty("webdriver.chrome.driver","C:\\Users\\Matias\\Downloads\\chromedriver-win64\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.google.co.uk/");
driver.manage().window().maximize();
} }
return driver; return driver;

View file

@ -1,34 +1,51 @@
package test; package test;
import main.ConfigureDriver;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.By; import org.openqa.selenium.By;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.File; // Import the File class
import java.io.IOException; // Import the IOException class to handle errors
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxOptions;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class testlogin { public class testlogin {
String usernameText = "testuser";
String passwordText = "QUBsucks&UUisBetter";
String loginPage = "https://test.fusil.uk/wp-login.php";
String usernameBoxID = "user_login";
String passwordBoxID = "user_pass";
String loginButtonID = "wp-submit";
String expectedUrl = "https://test.fusil.uk/wp-admin/";
@Test @Test
void testLogin() { void testLogin() {
//begin chromedriver code WebDriver driver = ConfigureDriver.configureDriver();
System.setProperty("webdriver.gecko.driver", "/usr/local/bin/geckodriver");
FirefoxOptions options = new FirefoxOptions();
options.setBinary("/usr/bin/firefox"); // Explicitly set Firefox binary
WebDriver driver = new FirefoxDriver(options);
//end chromedriver code //end chromedriver code
driver.manage().window().maximize(); driver.manage().window().maximize();
driver.get("https://practicetestautomation.com/practice-test-login/"); driver.get(loginPage);
WebElement username = driver.findElement(By.id("username")); WebElement username = driver.findElement(By.id(usernameBoxID));
WebElement password = driver.findElement(By.id("password")); WebElement password = driver.findElement(By.id(passwordBoxID));
WebElement login = driver.findElement(By.id("submit")); WebElement login = driver.findElement(By.id(loginButtonID));
username.sendKeys("student"); username.sendKeys(usernameText);
password.sendKeys("Password123"); password.sendKeys(passwordText);
login.click(); login.click();
String expectedUrl = "https://practicetestautomation.com/logged-in-successfully/";
assertEquals(expectedUrl, driver.getCurrentUrl()); assertEquals(expectedUrl, driver.getCurrentUrl());
driver.quit(); driver.quit();
} }
} // public static void main(String[] args) {
// try {
// File myObj = new File("filename.txt");
// if (myObj.createNewFile()) {
// System.out.println("File created: " + myObj.getName());
// } else {
// System.out.println("File already exists.");
// }
// } catch (IOException e) {
// System.out.println("An error occurred.");
// e.printStackTrace();
// }
// }
}