2013-05-12 28 views
6

Vì vậy, tôi đang thử nghiệm với tự động hóa Selenium, và tôi đang cố gắng viết một testcase đăng nhập, đi đến một trang cụ thể, nhập dữ liệu, sau đó nhấn gửi. Vấn đề là khi nó chạy, nó loại ra các thông tin, nhấn "Gửi" các trang web trả về:Chạy thử nghiệm selenium sẽ không lưu cookie?

Trang web này sử dụng cookie HTTP để xác minh thông tin ủy quyền. Vui lòng bật cookie HTTP để tiếp tục.

Nhưng sau đó khi tôi đã thêm dòng này [ký hiệu bằng // 1]:

driver.findElement(By.cssSelector("p > input[type=\"submit\"]")).click(); 

Nó cho phép đăng nhập phải đi qua cho đến khi nó được cho là trang gửi tin nhắn [ký hiệu bằng // 2] , nó yêu cầu thông tin đăng nhập một lần nữa (như thể không có đăng nhập đã từng được thực hiện). Vậy firefox có chấp nhận cookie không? Làm thế nào để sửa lỗi này?

Nguồn:

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.JUnitCore; 
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxDriver; 

import java.util.concurrent.TimeUnit; 

import static org.junit.Assert.assertEquals; 
import static org.junit.Assert.fail; 

public class LaPwn { 
    private WebDriver driver; 
    private String baseUrl; 
    private boolean acceptNextAlert = true; 
    private StringBuffer verificationErrors = new StringBuffer(); 
    private String UserID = ""; 
    private String UserPW = ""; 
    private String UserPIN = ""; 

    public static void main(String[] args) throws Exception { 

     UserInfo User = new UserInfo(); 

     User.setUserInfo(); 

     System.out.println(User.getUserID()); 
     System.out.println(User.getUserPW()); 
     System.out.println(User.getUserPIN()); 


     JUnitCore.main("LaPwn"); 
    } 

    @Before 
    public void setUp() throws Exception { 
     driver = new FirefoxDriver(); 
     baseUrl = "https://my_url.com"; 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 

    @Test 
    public void testLaPwn() throws Exception { 
     driver.get(baseUrl + "/Login"); 
     //1 
     driver.findElement(By.cssSelector("p > input[type=\"submit\"]")).click(); 
     //1 
     driver.findElement(By.id("UserID")).clear(); 
     driver.findElement(By.id("UserID")).sendKeys("User.getUserID()"); 
     driver.findElement(By.name("PIN")).clear(); 
     driver.findElement(By.name("PIN")).sendKeys("User.getUserPW()"); 
     driver.findElement(By.cssSelector("p > input[type=\"submit\"]")).click(); 
     driver.findElement(By.id("apin_id")).sendKeys("User.getUserPIN()"); 
     driver.findElement(By.cssSelector("div.pagebodydiv > form > input[type=\"submit\"]")).click(); 

     //2 
     driver.get(baseUrl + "/messagecenter"); 
     //2 
     try { 
      assertEquals("Send message:", driver.getTitle()); 
     } catch (Error e) { 
      verificationErrors.append(e.toString()); 
     } 
     driver.findElement(By.id("user")).clear(); 
     driver.findElement(By.id("user")).sendKeys("test123"); 
     driver.findElement(By.id("messg")).clear(); 
     driver.findElement(By.id("messg")).sendKeys("Hello test123!"); 
     driver.findElement(By.xpath("(//input[@name='SEND_BTN'])[2]")).click(); 
    } 

    @After 
    public void tearDown() throws Exception { 
     driver.quit(); 
     String verificationErrorString = verificationErrors.toString(); 
     if (!"".equals(verificationErrorString)) { 
      fail(verificationErrorString); 
     } 
    } 

    private boolean isElementPresent(By by) { 
     try { 
      driver.findElement(by); 
      return true; 
     } catch (NoSuchElementException e) { 
      return false; 
     } 
    } 

    private boolean isAlertPresent() { 
     try { 
      driver.switchTo().alert(); 
      return true; 
     } catch (NoAlertPresentException e) { 
      return false; 
     } 
    } 

    private String closeAlertAndGetItsText() { 
     try { 
      Alert alert = driver.switchTo().alert(); 
      String alertText = alert.getText(); 
      if (acceptNextAlert) { 
       alert.accept(); 
      } else { 
       alert.dismiss(); 
      } 
      return alertText; 
     } finally { 
      acceptNextAlert = true; 
     } 
    } 
} 
+0

Bạn đã thử thêm một số chờ ở đó chưa? – acdcjunior

Trả lời

8

Dựa trên báo cáo vấn đề của bạn, vấn đề bạn đang phải đối mặt là selen đang mở một hồ sơ firefox tươi mà cookie chưa được bật.

driver = new FirefoxDriver(); Đây là nơi bạn phải khắc phục theo cách mở ra hồ sơ nơi cookie được bật. Một cách là tạo tiểu sử của riêng bạn trong firefox và mở hồ sơ đó thay vì mở trực tiếp bởi FirefoxDriver();

ProfilesIni profileObj = new ProfilesIni(); 
FirefoxProfile yourFFProfile = profileObj.getProfile("your profile"); 
driver = new FirefoxDriver(yourFFProfile); 

Bằng cách này bạn có thể thực hiện những gì bạn cần trong hồ sơ đó và chạy thử nghiệm trong cài đặt đó. Nếu bật cookie là cần thiết, hãy làm điều đó trong tùy chọn firefox.

Sau đây là một cách khác để mở một cấu hình cụ thể theo seleniumhq.org

File profileDir = new File("path/to/top/level/of/profile"); 
FirefoxProfile profile = new FirefoxProfile(profileDir); 
profile.addAdditionalPreferences(extraPrefs); 
WebDriver driver = new FirefoxDriver(profile); 

Kiểm tra nguồn để biết thêm về chủ đề này. Nguồn: http://docs.seleniumhq.org/docs/03_webdriver.jsp#modifying-the-firefox-profile

+0

Để xem/tạo cấu hình trong Windows, bạn có thể định vị firefox.exe. Mở một dấu nhắc lệnh tại vị trí của firefox.exe và chạy firefox.exe -ProfileManager để mở một cửa sổ nhỏ để quản lý các cấu hình. Gợi ý: firefox thường nằm trong "C: \ Program Files (x86) \ Mozilla Firefox \ firefox.exe" hoặc "C: \ Program Files \ Mozilla Firefox \ firefox.exe". Với thư mục mở, hãy di chuyển chuột phải vào vùng trắng và chọn "cửa sổ lệnh mở tại đây". Trong câu trả lời của Phật, nó sẽ tìm kiếm một hồ sơ gọi là "hồ sơ của bạn". – weezilla

1

tôi thực sự nhận ra tôi định nghĩa URL cơ sở như

baseUrl = "https://my_url.com/"; 

và nó đã concatenating nó thích:

driver.get(baseUrl + "/Login"); 

cho "https://my_url.com//Login". Cảm ơn bạn đã chơi lại!

+0

+1 cảm ơn - điều đó cực kỳ đơn giản, tôi đã thử mọi thứ khác để giải quyết vấn đề này trong một vài ngày :) – msanjay

Các vấn đề liên quan