diff --git a/pom.xml b/pom.xml index 54175bd..1b3d3f4 100644 --- a/pom.xml +++ b/pom.xml @@ -16,10 +16,36 @@ - junit - junit - 3.8.1 + org.seleniumhq.selenium + selenium-java + 4.10.0 + + + + io.github.bonigarcia + webdrivermanager + 5.4.0 + + + + org.testng + testng + 7.8.0 test + + + org.springframework + spring-jdbc + 6.1.4 + + + + com.mysql + mysql-connector-j + 8.3.0 + + + diff --git a/src/main/java/jdbc/ConnectionFactory.java b/src/main/java/jdbc/ConnectionFactory.java new file mode 100644 index 0000000..36541b0 --- /dev/null +++ b/src/main/java/jdbc/ConnectionFactory.java @@ -0,0 +1,29 @@ +package jdbc; + +import java.io.FileInputStream; +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +public class ConnectionFactory { + + public Connection getConnection() throws SQLException, ClassNotFoundException, IOException { + String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); + FileInputStream fileInputStream = new FileInputStream(rootPath + "credentials.property"); + Properties prop = new Properties(); + try { + prop.load(fileInputStream); + } finally { + fileInputStream.close(); + } + + + Class.forName(prop.getProperty("driverClassName")); + + return DriverManager.getConnection( + prop.getProperty("url"), prop.getProperty("username"), prop.getProperty("password")); + + } +} diff --git a/src/main/java/web_driver/WebDriverFactory.java b/src/main/java/web_driver/WebDriverFactory.java new file mode 100644 index 0000000..b1efff3 --- /dev/null +++ b/src/main/java/web_driver/WebDriverFactory.java @@ -0,0 +1,27 @@ +package web_driver; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.edge.EdgeDriver; +import org.openqa.selenium.edge.EdgeOptions; + +public class WebDriverFactory { + + public WebDriver getDriver(){ + System.setProperty("webdriver.edge.driver", "src/main/resources/msedgedriver.exe"); + return new EdgeDriver(edgeOptions()); + } + + public EdgeOptions edgeOptions(){ + EdgeOptions edgeOptions = new EdgeOptions(); + + edgeOptions.setHeadless(true); + edgeOptions.addArguments("--disable-gpu"); + edgeOptions.addArguments("--window-size=1920,1200"); + edgeOptions.addArguments("--ignore-certificate-errors"); + edgeOptions.addArguments("--silent"); + edgeOptions.addArguments("--start-maximized"); + + return edgeOptions; + } + +} \ No newline at end of file diff --git a/src/test/java/dbtest/DbTest.java b/src/test/java/dbtest/DbTest.java new file mode 100644 index 0000000..75ec690 --- /dev/null +++ b/src/test/java/dbtest/DbTest.java @@ -0,0 +1,59 @@ +package dbtest; + +import jdbc.ConnectionFactory; +import org.checkerframework.checker.units.qual.C; +import org.springframework.jdbc.core.JdbcTemplate; +import org.testng.Assert; +import org.testng.annotations.*; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class DbTest { + + ConnectionFactory factory = new ConnectionFactory(); + + Connection connection; + Statement statement; + + @BeforeSuite + public void getConnection() throws SQLException, ClassNotFoundException, IOException { + connection = factory.getConnection(); + statement = connection.createStatement(); + } + + @AfterSuite + public void closeConnection() throws SQLException { + connection.close(); + } + + @Test + public void getUserFromDBTest() throws SQLException { + ResultSet resultSet = statement.executeQuery("SELECT * FROM mydb.user WHERE id = 1"); + resultSet.next(); + Assert.assertEquals(resultSet.getString(2), "yury"); + } + + @Test + public void updateUser() throws SQLException { + int result = statement.executeUpdate("UPDATE user set name='valera' WHERE id = 3"); + Assert.assertEquals(result, 1); + } + + @Test + public void createUser() throws SQLException { + statement.execute("INSERT INTO user (`name`, `surname`, `age`, `salary`) " + + "VALUES ('pasha', 'rock', '4', '9993')"); + Assert.assertTrue(statement.execute("SELECT * FROM user WHERE name = 'pasha'")); + } + + @Test + public void deleteUser() throws SQLException { + int result = statement.executeUpdate("DELETE FROM user WHERE name = 'pasha'"); + Assert.assertEquals(result, 1); + } + +} diff --git a/src/test/java/utils/BaseTest.java b/src/test/java/utils/BaseTest.java new file mode 100644 index 0000000..50e49d7 --- /dev/null +++ b/src/test/java/utils/BaseTest.java @@ -0,0 +1,22 @@ +package utils; + +import org.openqa.selenium.WebDriver; +import org.testng.annotations.AfterSuite; +import org.testng.annotations.BeforeMethod; +import web_driver.WebDriverFactory; + +public class BaseTest { + + protected static WebDriver driver; + + @BeforeMethod + public void setUp(){ + WebDriverFactory factory = new WebDriverFactory(); + driver = factory.getDriver(); + } + + @AfterSuite + public void wrapUp(){ + driver.quit(); + } +}