Pesquisar este blog

segunda-feira, 24 de dezembro de 2012

Exemplo de Cadastro de Dados usando o Selenium RC no Eclipse.

Precisei automatizar um processo lento de cadastro de MAC em um sistema Web, estava me custando bastante tempo para realização do mesmo, bom primeiro ele remove o MAC se estiver cadastrado em um lista e inseri em uma nova lista já cadastrada, isso obedece um conjunto de passos até realizar a operação. Segue abaixo os passos de forma genérica, para criação do mesmo em qualquer sistema Web.

Passos:
1. Abrir o Eclipse2. Instalar o Selenium RC


3. Instalar a biblioteca POI


4. Instalar Selenium IDE


5. Exportar a gravação da operação que deseja realizar
6. Criar um projeto no Eclipse em seguida a classe.
7. Importar o código gerado pelo Selenium IDE.
8. Adaptar o código para pegar os valores que iram mudar cadastrados na Planilha.
9. Fazer os Loops ou IFs necessários.
10. Executar o Código.



Figura 1 - Mostra o Selenium IDE, aonde vai ser gravado o esqueleto do Script, depois de gravado os passos das atividades, será realizado a exportação para o Eclipse, no nosso caso "Java / Junit 4 / Remote Control".



A figura 2 - Mostra a criação de um projeto e uma classe chamada macCad.java, e também mostra as bibliotecas usadas.




A figura 3 - Mostra o Arquivo Excel contendo o Nome da Nova Lista e o MAC a ser cadastrado.


Código Fonte do nosso Script em Java + Selenium



------------------------------------------------------------------------------------------------------

package com.example.tests;

import static org.junit.Assert.*;
import com.thoughtworks.selenium.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.FileInputStream;
import java.util.regex.Pattern;

public class ScriptIAP extends SeleneseTestCase {
public String url="http://1.0.0.0:8080/";


@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", url);
selenium.start();
selenium.windowMaximize();
selenium.windowFocus();
//selenium.setSpeed("1000");
}

@Test
public void testMacCad() throws Exception {

selenium.setSpeed("1000");
selenium.open("/iap-provisioning/loginForm.jsp");

selenium.type("name=userName", "xxxx");
selenium.type("name=password", "yyyy");

selenium.click("css=input[type=\"submit\"]");

FileInputStream arquivoexcel_lote = new FileInputStream("C:\\Nome.xls");
selenium.waitForPageToLoad("90000");

HSSFWorkbook excel = new HSSFWorkbook(arquivoexcel_lote);
HSSFSheet topSheet = excel.getSheetAt(0);

/* Faz um for pegando a informação da linha do Arquivo do Excel.
O primeiro for pega automatico a quantidade de linhas e no segundo é inserido de forma manual
*/ 

//for (int i = 0; i < topSheet.getPhysicalNumberOfRows(); i++) { //for para incrementar 1 em 1 nas linhas
// Faz um for pela quantidade especificada no caso 10 linhas.
for (int i = 0; i < 10; i++) { 
HSSFRow thirdRow = topSheet.getRow(i); //linhas da planilha
HSSFCell caminho_lote = thirdRow.getCell(1); //definindo a coluna 2
HSSFCell user = thirdRow.getCell(0); //definindo a coluna 1

selenium.click("link=Search");
selenium.waitForPageToLoad("130000");
selenium.type("xpath=(//input[@name='search'])[6]", caminho_lote.getStringCellValue());
selenium.click("xpath=(//input[@value='Search'])[6]");
selenium.waitForPageToLoad("130000");
if (selenium.isElementPresent("link=exact:"+ caminho_lote.getStringCellValue())){

selenium.click("link=exact:"+ caminho_lote.getStringCellValue());
selenium.waitForPageToLoad("130000");
selenium.click("link=View subscriber");
selenium.waitForPageToLoad("130000");
selenium.click("link=exact:"+caminho_lote.getStringCellValue());
selenium.waitForPageToLoad("130000");
selenium.click("link=Delete equipment");
assertTrue(selenium.getConfirmation().matches("^Are you sure you want to delete this equipment[\\s\\S]quot;));

}

selenium.click("link=Search");
selenium.waitForPageToLoad("130000");
selenium.type("name=search",user.getStringCellValue());
selenium.click("css=input[type=\"submit\"]");
selenium.waitForPageToLoad("130000");
selenium.click("link="+user.getStringCellValue());
selenium.waitForPageToLoad("130000");
selenium.click("link=New equipment");
selenium.waitForPageToLoad("130000");
selenium.type("xpath=(//input[@name='id'])[2]", caminho_lote.getStringCellValue());
selenium.type("name=macAddress", caminho_lote.getStringCellValue());
selenium.type("name=name", caminho_lote.getStringCellValue());
selenium.select("name=defaultUserId", "label="+user.getStringCellValue());
selenium.click("name=/plainBootstrap[1]");
selenium.click("name=/plainBootstrap[1]/deviceSettings[1]");
selenium.click("name=/plainBootstrap[1]/deviceSettings[1]/authentication[1]");
selenium.type("name=/plainBootstrap[1]/deviceSettings[1]/authentication[1]/@id", user.getStringCellValue());
selenium.type("name=/plainBootstrap[1]/deviceSettings[1]/authentication[1]/@password", "1234");
selenium.click("name=/plainBootstrap[1]/noAuthentication[1]");
selenium.click("css=#radioButtonFalse > input[name=\"/plainBootstrap[1]/blocked[1]\"]");
selenium.click("css=input[type=\"submit\"]");
selenium.waitForPageToLoad("130000");
}

}

@After
public void tearDown() throws Exception {

selenium.stop();
}
}

Nenhum comentário:

Postar um comentário