0

嗨我想爲Chrome自動化AdvancedRestClient擴展來測試web服務。 我能夠啓動擴展和發送請求。但我無法得到任何迴應。Automationg AdvancedRestClient擴展爲鉻

public class WebServices { 
    public static void main(String[] args) { 
     //Start the driver 
     System.setProperty("webdriver.chrome.driver","$PATH_TO_DRIVER"); 
     ChromeOptions options = new ChromeOptions(); 
     options.addArguments("load-extension=C:/Users/$username/AppData/Local/Google/Chrome/User Data/Default/Extensions/hgmloofddffdnphfgcellkdfbfbjeloo/3.1.7_0"); 
     DesiredCapabilities capabilities = new DesiredCapabilities(); 
     capabilities.setCapability(ChromeOptions.CAPABILITY, options); 
     ChromeDriver driver = new ChromeDriver(capabilities); 

     driver.manage().deleteAllCookies(); 
     driver.manage().window().maximize(); 

     //Start the extension 
     driver.get("chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html"); 

     try { 
      Thread.sleep(10000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 

     //Get the authentication field and set authentication 
     WebElement authField = driver.findElement(By.xpath("//*[@id='appContainer']/div/div/div/div[4]/div[2]/section[1]/textarea")); 
     authField.sendKeys("$SET_AUTORIZATION") 

     //Get the reqestURL field and enter request 
     WebElement requestField = driver.findElement(By.xpath("//*[@id='appContainer']/div/div/div/div[2]/input")); 
     requestField.clear(); 
     requestField.sendKeys("$REQUEST"); 

     authField.click(); 

     //Click on send button 
     WebElement sendButton = driver.findElement(By.xpath("//*[@id='appContainer']/div/div/div/div[7]/div/button[2]")); 
     sendButton.click();   
    } 
} 

上面的步驟工作正常,當我手動做。但腳本不會產生任何迴應。 請幫忙。

回答

1

自動瀏覽器測試Web服務不是最可靠或最有效的方法。

您應該在您的測試中實例化一個HttpClient,而不是Webdriver實例。這將允許您直接進行REST調用,並以與通過WebDriver聲明相同的方式對響應進行交互處理。

這種方法需要幾毫秒而不是幾秒來運行測試。此外,它可以在任何地方運行,而無需安裝Chrome或Webdriver

+0

你能分享任何文檔或與上述方法相關嗎?另外我想知道爲什麼沒有響應產生。手冊正在運行,腳本失敗 – Sighil

+0

試試這個庫https://github.com/rest-driver/rest-driver –