2016-11-02 8 views
2

私は2つのクラスをjava testNGプロジェクトの同じパッケージに属していて、クラスAに 'webdriver driver'そのクラス、クロームが起動し、URLが開かれ、ユーザー名とパスワードが入力され、ログインボタンがクリックされました。 @BeforeClassアノテーションを使ってうまく働いた。WebdriverはFirefoxではなく、Firefoxでログインすることができます: 'オーナー文書を見つけることができませんでした'エラー

同じコードをクラスBにコピーし、ブラウザインスタンスをfirefoxに変更しましたが、「webdriverドライバはpublic staticとして宣言しました。 Firefoxが起動し、URLが開き、ユーザー名とパスワードが入力されましたが、ログインボタンがクリックまたは送信されませんでした。エラーでテストに失敗しました:

org.openqa.selenium.JavascriptException:エラー:所有するドキュメントが見つかりません。

私はこのエラーに遭遇したことがなく、「所有しているドキュメント」が参照されているかどうかわかりません。どちらかまたは両方のクラスのアクセスレベルと関係があると思われます。以下は2つのクラスからの抜粋です。何か不足していますか?

*

package com.example; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.testng.Assert; 
import org.testng.annotations.AfterClass; 
import org.testng.annotations.BeforeClass; 
import org.testng.annotations.Test; 

public class TheChrome { 
public static WebDriver driver; 


@BeforeClass(alwaysRun = true) 
public void launchBrowser() { 

driver = new ChromeDriver(); 
driver.get("http://www.example.com"); 
driver.manage().window().maximize(); 

@Test 
public void verifyLogin() throws InterruptedException { 

driver.findElement(By.id("username")).sendKeys("user"); 
driver.findElement(By.id("password")).sendKeys("password"); 
Thread.sleep(3000); 
driver.findElement(By.id("loginButton")).click(); 

*

package com.example; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.Assert; 
import org.testng.annotations.AfterClass; 
import org.testng.annotations.BeforeClass; 
import org.testng.annotations.Test; 

public class TheFirefox { 

public static WebDriver driver; 


@BeforeClass(alwaysRun = true) 
public void launchBrowser() throws InterruptedException { 

driver = new FirefoxDriver(); 

driver.get("http://www.example.com"); 
driver.manage().window().maximize(); 
Thread.sleep(3000);   
} 


@Test 
public void verifyLogin() throws InterruptedException { 

driver.findElement(By.id("username")).sendKeys("user"); 
driver.findElement(By.id("password")).sendKeys("password"); 
Thread.sleep(3000); 
driver.findElement(By.id("loginButton")).click(); 
+0

webdriverまたはfirefoxのブラウザを更新しようとしましたか? Firefoxのバージョンに問題があると思われます。 – HARDI

+0

@HARDI、firefoxとwebdriverの両方が最新です。入力したログイン情報でFirefoxとURLが正しく開きます。 JavascriptExceptionがスローされる前にクリックしないログインボタン –

+0

@HARDI JavaScriptExecutorを使用してクリックしようとすることができますか?次のようなものです。WebElement element = driver.findElement(By.id( "id")); JavascriptExecutorエグゼキュータ=(JavascriptExecutor)ドライバ。 executor.executeScript( "arguments [0] .click();"、要素); – Abhinav

答えて

0

これは、使用しているFirefoxバージョン49.0.2の問題です。それは不思議なケースで、使用中のブラウザのバージョンによってログイン機能が付与されている理由は不明ですが、問題を解決するにはバージョン46.0にダウングレードする必要があります。これで問題は解決しました。

0

編集:あなたは、関連するHTMLを含めるようにポストを編集する場合、これはを助けるために容易になるだろう。この非常に関連question and answer

を参照してください。

cssSelector:

ログインボタンを確認してください。インスペクタ内で、要素を右クリックしてCSSセレクタをコピーします。

driver.findElement(By.cssSelector("copypasta")).click(); 

あなたのボタンの内側のHTMLテキストは、「ログイン」である場合、例えば、この便利なcheat sheet.

に記載されているいくつかの異なる方法を使用してのxpathで検索してみてください。

driver.findElement(By.xpath("//button[contains(text(), 'Login']")).click(); 

がありますこれを行うためのさまざまな方法がありますので、あなたのhtmlを見れば人々があなたを助けるのに役立つと思います。