首页新闻找找看学习计划

Selenium 3 无法调用指定Firefox profile文件

0
悬赏园豆:5 [已关闭问题] 关闭于 2018-02-26 10:45

最近使用selenium webdriver 用Java语言写脚本测试登录功能. 碰到一个问题: 无法调用指定的Firefox Profile.

请教各位大神解决办法.

问题重现过程

1. 将注册和登录分别写在两段脚本并存储在两个单独的java文件中.

2. 运行注册脚本完成注册后, 使用注册的帐号及密码信息运行登录实例.

3. 登录失败,提示帐号密码错误. 但是如果将登录段脚本合并到注册脚本中就可以成功登录.

 

注册脚本:

  1 package com.qiujy.testweb_mvn; 
  2 import java.io.File;
  3 import java.io.IOException;
  4 import java.util.List;
  5 
  6 import org.apache.commons.io.FileUtils;
  7 import org.openqa.selenium.By;
  8 import org.openqa.selenium.Cookie;
  9 import org.openqa.selenium.OutputType;
 10 import org.openqa.selenium.TakesScreenshot;
 11 import org.openqa.selenium.WebDriver;
 12 import org.openqa.selenium.WebElement;
 13 import org.openqa.selenium.firefox.FirefoxDriver;
 14 import org.openqa.selenium.firefox.FirefoxOptions;
 15 import org.openqa.selenium.firefox.FirefoxProfile;
 16 import org.openqa.selenium.firefox.internal.ProfilesIni;
 17 import org.openqa.selenium.support.ui.ExpectedCondition;
 18 import org.openqa.selenium.support.ui.WebDriverWait;
 19 
 20 
 21 public class ProfileRegister {
 22     private static final String WebElement = null;
 23 
 24     /**
 25      * @param args
 26      * @throws InterruptedException
 27      * @throws IOException
 28      */
 29     public static void main(String[] args) throws InterruptedException, IOException {
 30            System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.19.1-win64\\geckodriver.exe");
 31            ProfilesIni pi = new ProfilesIni();
 32            FirefoxProfile profile = pi.getProfile("defaultqhj");
 33            FirefoxOptions options = new FirefoxOptions();
 34            options.setProfile(profile);
 35         WebDriver driver = new FirefoxDriver(options);
 36 ////           FirefoxProfile profile = new FirefoxProfile(new File("C:\\Users\\qianhj\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\as1bczf1.default-1513049377171"));
 37 //           FirefoxOptions options = new FirefoxOptions();
 38 //           options.setProfile(profile);
 39 //        WebDriver driver = new FirefoxDriver(options);
 40            
 41 //           String firefoxProfileDir="C:\\Users\\qianhj\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\lsntx5d6.defaultqhj";
 42 ////           FirefoxProfile profile = new FirefoxProfile(new File("C:\\Users\\qianhj\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\lsntx5d6.defaultqhj"));
 43 //           FirefoxProfile profile=new FirefoxProfile(new File(firefoxProfileDir));
 44 //           WebDriver driver = new FirefoxDriver(profile);
 45            
 46 
 47         driver.manage().window().maximize();
 48         driver.get("http://192.168.203.99:5000/#/login");   
 49         String sysTitle = driver.getTitle();
 50         String expectedTitle="Dva Demo";
 51         if (sysTitle.equals(expectedTitle)) {
 52             System.out.println(sysTitle);
 53         } else {
 54             System.out.println("Wrong Title");
 55         }
 56           
 57         //点“注册”,打开注册页面
 58         WebElement addButton=driver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div[5]/a"));
 59         addButton.click();
 60            Thread.sleep(1000);
 61            
 62         //输入注册信息
 63         WebElement userName=driver.findElement(By.xpath("//*[@id=\"nickname\"]"));
 64         userName.sendKeys("abc");
 65         WebElement password=driver.findElement(By.xpath("//*[@id=\"password\"]"));
 66         password.sendKeys("123456");
 67         WebElement cpassword=driver.findElement(By.xpath("//*[@id=\"confirm\"]"));
 68         cpassword.sendKeys("123456");
 69         WebElement mailbox=driver.findElement(By.xpath("//*[@id=\"email\"]"));
 70         mailbox.sendKeys("abc@000.com");
 71         WebElement add=driver.findElement(By.xpath("//*[@id=\"residence\"]"));
 72         add.click();
 73         WebElement add1=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/ul[1]/li[1]"));
 74         add1.click();    
 75         System.out.println(add1);
 76         WebElement add2=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/ul[2]/li"));
 77         add2.click();    
 78         System.out.println(add2);
 79         WebElement add3=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/ul[3]/li"));
 80         add3.click();    
 81         System.out.println(add3);
 82         WebElement cellphone=driver.findElement(By.xpath("//*[@id=\"phone\"]"));
 83         cellphone.sendKeys("123456789");    
 84  
 85         //获取验证码
 86         WebElement verifycode=driver.findElement(By.className("ant-btn"));
 87         verifycode.click();
 88         WebElement text=driver.findElement(By.className("ant-notification-notice-description"));
 89         String str=text.getText();
 90         String str1=str.replaceAll("请在5min内输入验证码:","");
 91         System.out.println(str1);
 92         WebElement inputcode=driver.findElement(By.xpath("//*[@id=\"captcha\"]"));
 93         inputcode.sendKeys(str1);
 94         WebElement checkbox=driver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div[8]/div/div/label/span[1]/input"));
 95         checkbox.click();
 96         File screenshotFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
 97         FileUtils.copyFile(screenshotFile, new File("E:\\screenshot\\Register"+System.currentTimeMillis()+".png"));
 98         WebElement register=driver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div[9]/div/div/button"));
 99         register.click();
100 
101         //取出注册成功的提示字样
102         WebElement text2=(new WebDriverWait(driver,10)).until(new ExpectedCondition<WebElement>() {
103             public org.openqa.selenium.WebElement apply(WebDriver driver) {
104                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
105                 if(eles.size() == 2 && eles.get(1).isDisplayed()) {
106                     return eles.get(1);
107                 }
108                 return null;
109             }
110         });
111 
112         System.out.println(text2.getText().toString());    
113 
114         Thread.sleep(1000);
115         File screenshotFile1=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
116         FileUtils.copyFile(screenshotFile1, new File("E:\\screenshot\\Success"+System.currentTimeMillis()+".png"));
117         
118         
119         
120         
121         
122         //用刚刚注册的帐号登录
123         WebElement uName=driver.findElement(By.xpath("//*[@id=\"userName\"]"));
124         uName.sendKeys("abc");
125         WebElement pwd=driver.findElement(By.xpath("//*[@id=\"password\"]"));
126         pwd.sendKeys("123456");    
127         WebElement Button=driver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div[4]/button"));
128         Button.click();
129         File screenshotFile2=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
130         FileUtils.copyFile(screenshotFile2, new File("E:\\screenshot\\login"+System.currentTimeMillis()+".png"));
131         
132         
133         
134         //取出登录成功的提示框title
135         WebElement text3=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
136             public org.openqa.selenium.WebElement apply(WebDriver driver){
137                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-message"));
138                 if(eles.size()==3 && eles.get(2).isDisplayed()) {
139                     return eles.get(2);
140                 }
141                 return  null;
142             }
143         });
144         System.out.println(text3.getText().toString());
145         
146         
147         
148         
149         //取出登录成功的提示字样
150         WebElement text4=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
151             public org.openqa.selenium.WebElement apply(WebDriver driver){
152                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
153                 if(eles.size()==3 && eles.get(2).isDisplayed()) {
154                     return eles.get(2);
155                 }
156                 return  null;
157             }
158         });
159         System.out.println(text4.getText().toString());
160             
161         File screenshotFile3=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
162         FileUtils.copyFile(screenshotFile3, new File("E:\\screenshot\\LS"+System.currentTimeMillis()+".png"));        
163         
164         Thread.sleep(10000);
165         
166         
167         
168          driver.quit();
169      
170     }
171 
172 }
View Code

登录脚本:

 1 package com.qiujy.testweb_mvn; 
 2 import java.io.File;
 3 import java.io.IOException;
 4 import java.util.List;
 5 
 6 import org.apache.commons.io.FileUtils;
 7 import org.openqa.selenium.By;
 8 import org.openqa.selenium.Cookie;
 9 import org.openqa.selenium.OutputType;
10 import org.openqa.selenium.TakesScreenshot;
11 import org.openqa.selenium.WebDriver;
12 import org.openqa.selenium.WebElement;
13 import org.openqa.selenium.firefox.FirefoxDriver;
14 import org.openqa.selenium.firefox.FirefoxOptions;
15 import org.openqa.selenium.firefox.FirefoxProfile;
16 import org.openqa.selenium.firefox.internal.ProfilesIni;
17 import org.openqa.selenium.support.ui.ExpectedCondition;
18 import org.openqa.selenium.support.ui.WebDriverWait;
19 
20 
21 public class ProfileLogin {
22     private static final String WebElement = null;
23 
24     /**
25      * @param args
26      * @throws InterruptedException
27      * @throws IOException
28      */
29     public static void main(String[] args) throws InterruptedException, IOException {
30            System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.19.1-win64\\geckodriver.exe");
31            ProfilesIni pi = new ProfilesIni();
32            FirefoxProfile profile = pi.getProfile("defaultqhj");
33            FirefoxOptions options = new FirefoxOptions();
34            options.setProfile(profile);
35         WebDriver driver = new FirefoxDriver(options);
36 
37 
38         driver.manage().window().maximize();
39         driver.get("http://192.168.203.99:5000/#/login");   
40         String sysTitle = driver.getTitle();
41         String expectedTitle="Dva Demo";
42         if (sysTitle.equals(expectedTitle)) {
43             System.out.println(sysTitle);
44         } else {
45             System.out.println("Wrong Title");
46         }
47           
48     
49         //用ProfileRegister 脚本中注册的帐号登录
50         WebElement uName=driver.findElement(By.xpath("//*[@id=\"userName\"]"));
51         uName.sendKeys("abc");
52         WebElement pwd=driver.findElement(By.xpath("//*[@id=\"password\"]"));
53         pwd.sendKeys("123456");    
54         WebElement Button=driver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div[4]/button"));
55         Button.click();
56         File screenshotFile2=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
57         FileUtils.copyFile(screenshotFile2, new File("E:\\screenshot\\login"+System.currentTimeMillis()+".png"));
58         
59         
60         
61         //取出登录成功的提示框title
62         WebElement text3=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
63             public org.openqa.selenium.WebElement apply(WebDriver driver){
64                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-message"));
65                 if(eles.size()==3 && eles.get(2).isDisplayed()) {
66                     return eles.get(2);
67                 }
68                 return  null;
69             }
70         });
71         System.out.println(text3.getText().toString());
72         
73         
74         
75         
76         //取出登录成功的提示字样
77         WebElement text4=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
78             public org.openqa.selenium.WebElement apply(WebDriver driver){
79                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
80                 if(eles.size()==3 && eles.get(2).isDisplayed()) {
81                     return eles.get(2);
82                 }
83                 return  null;
84             }
85         });
86         System.out.println(text4.getText().toString());
87             
88         File screenshotFile3=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
89         FileUtils.copyFile(screenshotFile3, new File("E:\\screenshot\\LS"+System.currentTimeMillis()+".png"));        
90         
91         Thread.sleep(10000);
92         
93         
94         
95          driver.quit();
96      
97     }
98 
99 }
View Code

 

问题根因

虽然脚本中定义了指定Firefox profile

1     public static void main(String[] args) throws InterruptedException, IOException {
2            System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.19.1-win64\\geckodriver.exe");
3            ProfilesIni pi = new ProfilesIni();
4            FirefoxProfile profile = pi.getProfile("defaultqhj");
5            FirefoxOptions options = new FirefoxOptions();
6            options.setProfile(profile);
7         WebDriver driver = new FirefoxDriver(options);

但是在运行过程中selenium 在temp文件夹中复制了一个profile文件,并且运行完毕后这里的信息并没有同步回指定的Firefox profile.

文字:

1514969522065mozrunner::runnerINFORunning command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\qianhj\\AppData\\Local\\Temp\\rust_mozprofile.CpsWZbn1Io5z"

钱先生的主页 钱先生 | 菜鸟二级 | 园豆:202
提问于:2018-01-03 17:32
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册