반응형




버거킹 영수증을 받아보면 하단에 설문조사코드가 있습니다.


설문을 마치면 단품으로 세트를 먹을 수 있는 코드를 발급받게 되는데요


설문조사를 하는 것도 은근히 시간이 걸리는 것 같아 한번 시도해보았습니다.


Selenium 을 사용하였고 스윙으로 UI를 대충 만들어보았습니다.


설문이 끝나면 발급코드가 표시되는 간단한 프로그램입니다.


자바 환경 설치가 필수로 되어 있어야 하며


C: 또는 D: 드라이브 경로에 

chromedriver.exe가 존재해야 합니다.


chromedriver.exe






시연 영상



http://youtu.be/NtjBOtUwknM



셀레니움 부분 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.burger.king;
 
import java.io.File;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
 
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import static com.codeborne.selenide.Selenide.*;
import static com.codeborne.selenide.Condition.*;
 
@FixMethodOrder (MethodSorters.NAME_ASCENDING)
 
public class survey {
    private static WebDriver driver;
    
    static String receiptNum="";
    static String url="https://kor.tellburgerking.com";
    
    
    public void inputNumber(String number){
          // 텍스트 필드값 가져오기
        receiptNum = number;
    }
 
 
    @BeforeClass
    public static void setUp() throws Exception {
        
        File file = new File("c:/chromedriver.exe");
        File file2 = new File("d:/chromedriver.exe");
           if(file.isFile()){         
               System.setProperty("webdriver.chrome.driver""c:/chromedriver.exe"); //크롬 드라이버 파일 경로설정
        }else if(file2.isFile()){
            System.setProperty("webdriver.chrome.driver""d:/chromedriver.exe"); //크롬 드라이버 파일 경로설정    
        }else{
            //안내문
            runner.labelIntroduce.setText("C 또는 D드라이브에 chromedriver가 없습니다.\n");
        }
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); //응답시간 5초설정
        driver.get(url); 
        
         driver.manage().window().maximize();
    }
    
    public static void login() throws InterruptedException   {
        boolean boo = true;
        WebElement NextButton = driver.findElement(By.id("NextButton"));
        NextButton.click();   
        
        driver.findElement(By.id("CN1")).sendKeys(receiptNum);  //ID
        Thread.sleep(1000);
        driver.findElement(By.id("NextButton")).click();
        
        
        int i = 0;
        while(boo){
            try{
                
                boolean radioButtonHolder = driver.findElements(By.className("radioButtonHolder")).size() > 0;
                boolean checkboxBranded = driver.findElements(By.className("checkboxBranded")).size() > 0;
                boolean radioBranded = driver.findElements(By.className("radioBranded")).size() > 0;
                boolean checkCode = driver.findElements(By.className("ValCode")).size() > 0;
                boolean Next = driver.findElements(By.id("NextButton")).size() > 0;
                 if(radioButtonHolder){
                     driver.findElement(By.className("radioButtonHolder")).click();
                 }
                 else if(checkboxBranded){
                     driver.findElement(By.className("checkboxBranded")).click();
                 }
                 else if(radioBranded){
                     driver.findElement(By.className("radioBranded")).click();
                 }
                 if(Next)
                     driver.findElement(By.id("NextButton")).click();
                 if(checkCode)
                     boo = false;
                 System.out.println(i++);
            }catch (NoSuchElementException e){
                
            }
           
        }
        
        String checkCode = driver.findElement(By.xpath("//*[@id='FNSfinishText']/div/p[2]")).getText();
 
        runner.labelIntroduce.setText(checkCode);
    }
    
 
    @Test
    public static void run() throws Exception {
        login();
        Thread.sleep(500);
    }
 
    @AfterClass
    public static void tearDown() throws Exception {
        driver.quit();
    }
 
}
cs



스윙UI 부분


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.burger.king;
 
import javax.swing.*;
 
import com.burger.king.runner;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
 
class runner extends Thread{
    Simulator simul = new Simulator();
    public static    JLabel labelIntroduce;
    public static survey survey = new survey();
    public  static  JTextField tf_receiptNum;
    public static JLabel submit,number;
    public boolean check = true
    public void run(){
        // 텍스트 필드값 가져오기
        String receiptNum = tf_receiptNum.getText();
        try {
            survey.setUp();
        } catch (Exception e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        survey.inputNumber(receiptNum);
        try {
            survey.run();
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    
}
 
class JPanel033 extends JPanel{        
    runner thread = new runner();
    
    // 클래스 멤버 필드 설정
    private    JLabel name;
    private    JLabel id;
            
    
    private    JButton buttonSave;
    private    JButton buttonStop;
    public boolean check = true
    
    public JPanel033() {         
        
        setLayout(null);
        
        // 라벨
        name = new JLabel("by. yonoo");
        name.setSize(10020);   
        name.setLocation(20);
        
        id = new JLabel("설문조사코드: ");
        id.setBounds(10,30,100,20);
        
        // 텍스트 필드
        runner.tf_receiptNum = new JTextField();             
        runner.tf_receiptNum.setBounds(100,30,200,20);
        
        thread.labelIntroduce = new JLabel("=");
        thread.labelIntroduce.setBounds(10,50,280,20);
       
            
        // 버튼        
        buttonSave = new JButton("시작");
        buttonSave.setBounds(80,80,100,20);
        buttonSave.addActionListener(new EventHandlerSave());   
        
        buttonStop = new JButton("정지");
        buttonStop.setBounds(220,80,100,20);
        buttonStop.addActionListener(new EventHandlerStop());   
        
        add(name);
        add(thread.labelIntroduce);
        
        add(id);
        add(thread.tf_receiptNum);
        
        add(buttonSave);
        add(buttonStop);
        
    }
    
    class EventHandlerSave implements ActionListener{     // 
        public void actionPerformed(ActionEvent e){
//            try {
//                thread.start();    
//            } catch (Exception e1) {
//                // TODO Auto-generated catch block
//                e1.printStackTrace();
//            }
            
            if(check){
                thread.start();    
            }else{
                runner thread = new runner();
                thread.start();    
                check = true;
            }
        }
    }   
    class EventHandlerStop implements ActionListener{     // 
        public void actionPerformed(ActionEvent e){
            check = false;
            System.out.println(check);
            thread.interrupt();
            System.out.println("정지!");
//            System.exit(1);
            
        }
    } 
    
}
 
 
public class Simulator extends JFrame{
    
    public JPanel033 jpanel03 = null;
   
    public static void main(String[] args) {
        Simulator win = new Simulator();
        
        win.setTitle("BurgerKing Survey Automation");
        win.jpanel03 = new JPanel033();
        
        URL imageURL = Simulator.class.getClassLoader().getResource("burger.png");
        System.out.println(imageURL);
        ImageIcon img = new ImageIcon(imageURL);
        win.setIconImage(img.getImage());
        
        win.add(win.jpanel03);
 
        win.setSize(400,150);
        win.setVisible(true);
        win.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        
    }
    
}
 
cs

반응형

+ Recent posts