51Testing软件测试论坛

标题: selenium WebDriver 写的for循环为什么不能循环执行呢,只能执行一次。 [打印本页]

作者: 18201153313    时间: 2016-7-21 14:04
标题: selenium WebDriver 写的for循环为什么不能循环执行呢,只能执行一次。
package com.brooke.test;

import static org.junit.Assert.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class sss {
        private static WebDriver driver=null;
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
                System.setProperty("webdriver.chrome.driver", "Files\\chromedriver.exe");
                driver=new ChromeDriver();
        }

        @AfterClass
        public static void tearDownAfterClass() throws Exception {
        }

        @Test
        public void test() {
                driver.get("http://192.168.5.14:8086/login");
               
               
                WebElement username=driver.findElement(By.name("username"));//识别用户名文本框元素
                WebElement password=driver.findElement(By.name("password"));//识别密码文本框元素
                WebElement captcha=driver.findElement(By.name("captcha"));//识别验证码输入框
                WebElement login=driver.findElement(By.className("clearfix"));//识别登录按钮
                char[] cha={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
                String s=null;
                for(int i=0;i<cha.length;i++){
                        username.sendKeys("haolei");
                        password.sendKeys("Htche123465");
                        s=String.valueOf(cha[i]);
                        System.out.println("验证码是"+s);
                        captcha.sendKeys(s);
                        login.submit();
                        System.out.println("第"+i+"次尝试");
                        System.out.println(cha.length);
                }
               
        }

}

作者: fhhh_eyou    时间: 2016-7-21 14:32
1.for循环写一次,执行一次。
2.多次循环for多写一次实现。
作者: seagull1985    时间: 2016-7-21 16:04
我帮你跑了一次是没有问题的,应该是以下几行代码抛异常了,你自己try catch下。。。
username.sendKeys("haolei");
password.sendKeys("Htche123465");

captcha.sendKeys(s);
login.submit();




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2