Skip to content

Commit

Permalink
fix) 셀레니움이 정상적으로 실행되지 않던 문제
Browse files Browse the repository at this point in the history
  • Loading branch information
migusdn committed Oct 29, 2020
1 parent 3368d0d commit ed48c00
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 25 deletions.
34 changes: 34 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jackson.version>2.11.1</jackson.version>
<junit.version>4.12</junit.version>
<log4j.version>2.13.3</log4j.version>
<slf4j.version>1.7.25</slf4j.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -66,7 +68,39 @@
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.apple/AppleJavaExtensions -->
<dependency>
<groupId>com.apple</groupId>
<artifactId>AppleJavaExtensions</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/migusdn/EzMacro/App/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.nio.charset.StandardCharsets;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.migusdn.EzMacro.Protocol.NativeRequest;
Expand Down Expand Up @@ -46,12 +47,12 @@ private static String readMessage(InputStream in) throws IOException {
b = new byte[size];
in.read(b);

return new String(b, "UTF-8");
return new String(b, StandardCharsets.UTF_8);
}

private static void sendMessage(String message) throws IOException {
System.out.write(getBytes(message.length()));
System.out.write(message.getBytes("UTF-8"));
System.out.write(message.getBytes(StandardCharsets.UTF_8));
System.out.flush();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/migusdn/EzMacro/App/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class Window implements Runnable{
//singleton frame
private static JFrame frame = new JFrame("EzMacro");
private static final JFrame frame = new JFrame("EzMacro");

public static JFrame getFrame(){ return frame;}
@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/migusdn/EzMacro/Enum/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public enum Command {
mouseOver,
doubleClick,
loop,
selectFrame
selectFrame,
mouseOut,
mouseUp
}
3 changes: 2 additions & 1 deletion src/main/java/com/migusdn/EzMacro/Enum/TargetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum TargetType {
xpath,
linkText,
name,
time
time,
id
}

2 changes: 1 addition & 1 deletion src/main/java/com/migusdn/EzMacro/GUI/GUI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.migusdn.EzMacro.GUI;

public interface GUI {
public void validate();
void validate();
}
2 changes: 1 addition & 1 deletion src/main/java/com/migusdn/EzMacro/GUI/Main_UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Main_UI implements GUI{
private JPanel panel1;
private JButton newMacroButton;
private JButton importMacroButton;
private JFileChooser chooser;
private final JFileChooser chooser;
public Main_UI() {
chooser = new JFileChooser();
newMacroButton.addActionListener(new ActionListener() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/migusdn/EzMacro/GUI/TaskList_UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class TaskList_UI implements GUI{
private JRadioButton changeFrameRadioButton;
private JComboBox comboBox1;
private ArrayList<TaskElement> elementList = new ArrayList<TaskElement>();
private static ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectMapper objectMapper = new ObjectMapper();
public void setElementList(ArrayList<TaskElement> TElement){
this.elementList = TElement;
for(int i=0; i<elementList.size();i++){
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/migusdn/EzMacro/GUI/menu.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.migusdn.EzMacro.GUI.menu">
<grid id="27dc6" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<toolbar id="772cf">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="20"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children/>
</toolbar>
<vspacer id="eadd1">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</form>
4 changes: 4 additions & 0 deletions src/main/java/com/migusdn/EzMacro/GUI/menu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.migusdn.EzMacro.GUI;

public class menu {
}
4 changes: 2 additions & 2 deletions src/main/java/com/migusdn/EzMacro/Util/JsonUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.Iterator;

public class JsonUtility {
private static ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper mapper = new ObjectMapper();

public static Task ToTask(String JsonString) throws JsonProcessingException {
Task TaskObj = mapper.readValue(JsonString, Task.class);
Expand All @@ -42,7 +42,7 @@ public static Task ImportFile(String FilePath) throws IOException {
Command command = Command.valueOf(tmp.get("command").toString());
System.out.println(tmp.get("command"));
String[] target = tmp.get("target").toString().split("=");
if(command==Command.open||command==command.runScript){
if(command==Command.open||command== Command.runScript){
TElement.add(
new TaskElement(command,
null,
Expand Down
40 changes: 25 additions & 15 deletions src/main/java/com/migusdn/EzMacro/Util/SeleniumUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
import java.util.Iterator;

public class SeleniumUtility implements Runnable{
JavascriptExecutor js;
private WebDriver driver;
private Task task;
private static JavascriptExecutor js;
private static WebDriver driver;
private final Task task;

private String WEB_DRIVER_ID = "webdriver.chrome.driver";
private String WEB_DRIVER_PATH = System.getProperty("user.home")+ File.separator+"chromedriver";
private final String WEB_DRIVER_ID = "webdriver.chrome.driver";
private final String WEB_DRIVER_PATH = System.getProperty("user.home")+ File.separator+"chromedriver";

public SeleniumUtility(Task task){
super();
Expand All @@ -41,14 +41,15 @@ public SeleniumUtility(Task task){
public void run() {
System.setProperty(WEB_DRIVER_ID, WEB_DRIVER_PATH);
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
try {
//get page (= 브라우저에서 url을 주소창에 넣은 후 request 한 것과 같다)
driver.get(task.getTarget_url());
//driver.get(task.getTarget_url());
//System.out.println(driver.getPageSource());
Iterator<TaskElement> taskList= task.getTaskList().iterator();
//command에 따라서 한번, target에 따라서 한번
while(taskList.hasNext()){

commandExec(taskList.next());
}

} catch (Exception e) {
Expand All @@ -61,7 +62,9 @@ public void run() {
}

}
public void commandExec(TaskElement TElement){
public void commandExec(TaskElement TElement) throws NullPointerException{
TargetType targetType = TElement.getTargetType();
String target = TElement.getTarget();
switch(TElement.getCommand()){
case open:{
driver.get(task.getTarget_url());
Expand All @@ -71,43 +74,50 @@ public void commandExec(TaskElement TElement){
break;
}
case click:{
targetExec(TElement.getTargetType(), TElement.getTarget()).click();
driver.findElement(targetExec(targetType, target)).click();
break;
}
case mouseOver:{
break;
}
case runScript: {
js.executeScript(TElement.getTarget());
js.executeScript(target);
break;
}
case doubleClick:{
Actions builder = new Actions(driver);
// builder.doubleClick(targetExec(targetType,target)).perform();
break;
}
case setWindowSize:{
break;
}
//index의 경우 간단히 전환하면 가능함
case selectFrame:{
driver.switchTo().frame(Integer.parseInt(target));
break;
}
}
}
public WebElement targetExec(TargetType targetType, String target){
public By targetExec(TargetType targetType, String target){
switch(targetType){
case css:{
return driver.findElement(By.cssSelector(target));
return By.cssSelector(target);
}
case name:{
return driver.findElement(By.name(target));
return By.name(target);
}
case time:{
break;
}
case xpath:{
return driver.findElement(By.xpath(target));
return By.xpath(target);
}
case linkText:{
return driver.findElement(By.linkText(target));
return By.linkText(target);
}
case id:{
return By.id(target);
}
}
return null;
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/log4j.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>

<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p: %c - %m%n" />
</Console>
</Appenders>

<Loggers>
<Root level="INFO">
<AppenderRef ref="console" />
</Root>
</Loggers>

</Configuration>

0 comments on commit ed48c00

Please sign in to comment.