2016-06-29 3 views
-1

私はセレンでロジックを実装しました。ユーザIDが テストケースが に失敗しかし、私になりますが存在しない場合は は、要件がユーザーIDは、それがログインページに移動しますが存在する場合 の下にExcelシート からユーザーIDとパスワードを取得している要素を探しアプリケーション をPageHeader_closeユーザーはその投げ要素PageHeader_closeアプリケーションを存在しないためにidのコードではないの は、実際に私は、ログインが成功しなかった場合は、それ自体が故障した場合にしたい存在下に セレンでのログインのロジック

@Test(dataProvider = "injectdataintovsslogin") 
public void injectdataintowebsite(String username, String password) throws InterruptedException 
{ 
    System.setProperty("webdriver.ie.driver", "C:\\Eclipse\\IEDriverServer.exe"); 
    DesiredCapabilities caps = new DesiredCapabilities(); 
    caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); 
    driver = new InternetExplorerDriver(caps); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    driver.manage().window().maximize(); 
    driver.get("http://segotn11540.rds.scania.com/vss_connect_testr1/Login/Login.aspx"); 
    driver.findElement(By.xpath("//input[@name='UserNameInputText']")).sendKeys(username); 
    driver.findElement(By.xpath("//input[@name='Brand']")).sendKeys(password); 
    driver.findElement(By.xpath("//input[@name='CmdLogin']")).click(); 
    isuserloggedin(); 
} 

public boolean isuserloggedin() 
{ 
    boolean flag = true; 
    if (!driver.findElements(By.id("Cancel")).isEmpty()) 
    { 
     driver.findElement(By.id("Cancel")).click(); 
    } 
    WebDriverWait wait = new WebDriverWait(driver, 10); 
    WebElement link = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("PageHeader_CloseApplication")))); 
    if (!link.isDisplayed()) 
    { 
     flag = false; 
     System.out.println("User InCorrect"); 
    } 
    else 
    { 
     System.out.println("User Incorrect"); 
    } 
    return flag; 
} 

@AfterMethod 
public void teardown() 
{ 
    driver.quit(); 
} 

@DataProvider(name = "injectdataintovsslogin") 
public Object[][] wordpressinjectprofile() throws Exception 
{ 
    ReadExcelConfig file = new ReadExcelConfig("C:\\Code\\Test Data.xlsx"); 
    int rows = file.rowcount(0); 
    Object[][] getdata = new Object[rows][2]; 
    for (int i = 0; i < rows; i++) 
    { 
     getdata[i][0] = file.getdata(0, i, 0); 
     getdata[i][1] = file.getdata(0, i, 1); 
    } 
    return getdata; 
} 
+1

あなたの質問がありますか?私はExcelシート からユーザーIDを引っ張っています – JeffC

+0

@Jeff私は、ユーザーがボタンをキャンセルした場合 a)はホーム・ページ B)に見える行くが、要素の確認が存在する 以下のように1.Ifロジックを実装したいですPageHeader_closeアプリケーション 2) ユーザーがすぐに存在しない場合は、テストケースに失敗します。 ユーザーが存在しない場合、私はfind要素を追加したので例外がスローされます ユーザーが存在しない場合要素が見つからない場合 コードを変更するにはどうすればよいですか –

+0

すべてのエキスパートアンサー –

答えて

0

私が思う私のコードです制御フローロジックに問題があります。 if X then Yタイプのもの... ifに入る必要がある声明があると思いますが、どのものがまだわかりませんが、私は助けようとします。問題は、コード化されていないときに何が起こるかを知るための十分な情報がないことです。まず、コードにラベルを付けることです。コードフローを説明するたびにコメントを入れてください。たとえば、ifelseの後に// customer successfully logged inという短いコメントを付けます。これは、あなた(そして他の人たち)がUIで何が起こっているのかを記憶し、その流れに従うのに役立ちます。

私はあなたのisuserloggedin()機能に焦点をあてるつもりです。なぜなら、それは問題だと思うからです。まず、Javaの規約に従い、キャメルケースを使用して名前をisUserLoggedIn()に変更します。 Java conventionsを参照してください。私はいくつかのコメントと質問を以下のコードに入れました。それらを見て、質問に答えてください、私はそれに応じて私の答えを編集します。

ユーザーがログインしているかどうかを確認するにはどうすればよいですか?

public boolean isUserLoggedIn() 
{ 
    boolean flag = true; 
    List<WebElement> cancelButton = driver.findElements(By.id("Cancel")); // fetch this once and use it twice, below 
    if (!cancelButton.isEmpty()) 
    { 
     // what does it mean when the Cancel button exists? User is logged in or not or ??? 
     cancelButton.get(0).click(); 
    } 
    WebDriverWait wait = new WebDriverWait(driver, 10); 
    WebElement link = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("PageHeader_CloseApplication")))); 
    if (!link.isDisplayed()) // you waited for visibility above, you shouldn't need to check isDisplayed() here 
    { 
     // what does it mean if the link is missing? User isn't logged in or ? 
     flag = false; 
     // why is this sysout the same as the below? I'm guessing this is supposed to be "User is NOT logged in"? 
     System.out.println("User InCorrect"); 
    } 
    else 
    { 
     // why is this sysout the same as the above? I'm guessing this is supposed to be "User is logged in"? 
     System.out.println("User Incorrect"); 
    } 
    return flag; 
} 
+0

こんにちはJeff、 あなたのご意見ありがとうございます。 は、実際に私はタイトル を主張するためのロジックを変更するので、新しいロジックが サインイン以下のようになり、ログインタイトルはログインが一致した後の場合は、タイトル を得る成功し あなたはサイトに新しいしているので、これは実際に私 –

+0

ために働きました。 ..あなたが役に立つ答えを見つけたら、それをupvoteする必要があります。答えがあなたの質問に答えたら、それを受け入れるべきです。それは、調査と質問に時間を費やす人々への小さな報酬です。 – JeffC

関連する問題