I am automating a webform on a webpage. I am still learning Selenium on the road. I know a few bad/good practices but a code review is always a good thing to do. What should I improve on in my code?
Quick explanation of the code:
- I read a JSON file with customers out.
- Initializing some variables on the run that are needed for the form.
- For each customer get product information (data).
- Filling the form with data using Selenium (actions).
- After that I update a field in my database.
The code:
public void fillOutForm() throws Exception {
String path = "D:\\Users\\Administrator\\eclipse-workspace\\AdAutomation\\data\\json\\users\\";
String filename = username;
String extension = ".json";
setPath(path, filename, extension);
String file = getPath();
json.Customer customers = JSONReader.getCustomers(file);
int products = customers.getProducts().size();
int duration_in_hours = 2;
int interval_in_minutes = 5;
setInterval(duration_in_hours, interval_in_minutes, products);
int turns = db.getTurns(customers.getId());
if(customers != null) {
for(json.Product data : customers.getProducts()) {
waitForLoad(driver);
// ***********************************************************************************************
driver.navigate().to(URL + "/syi/" + data.getGroupId() + "/" + data.getRubricId() + "/plaatsAdvertentie.html");
Thread.sleep(150);
// ***********************************************************************************************
WebElement ui_upload = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("content")));
ui_upload.click();
// ***********************************************************************************************
String images = data.getImages();
try{
x.controlFocus("Bestand uploaden", "", "Edit1"); //File Upload
x.ControlSetText("Bestand uploaden", "", "Edit1", images);
x.controlClick("Bestand uploaden", "", "Button1");
} catch(Exception e) {
System.out.println(e.getMessage());
}
WebElement ui_wait_for_upload = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("reject-blik")));
ui_wait_for_upload.click();
// ***********************************************************************************************
WebElement ui_title = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("title")));
ui_title.clear();
ui_title.sendKeys(data.getTitle().toString() + " #" + turns);
// ***********************************************************************************************
Thread.sleep(150);
WebElement ui_condition = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*/div/select[@name='attribute[condition]']")));
Select select = new Select(ui_condition);
select.selectByValue(data.getConditionId().toString());
Thread.sleep(150);
// ***********************************************************************************************
WebElement ui_zip = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("contactInformation.postCode")));
ui_zip.clear();
ui_zip.sendKeys(data.getZip());
// ***********************************************************************************************
String myString = data.getDescription();
byte[] utf8Bytes = myString.getBytes("UTF8");
String description = new String(utf8Bytes,"UTF8");
description = description.replace("\r\n", "");
String id = "<b>#" + turns + "</b><br>";
String nr = "<br><b>Artikelnummer: " + data.getNumber() + "</b>.<br><br>";
String message = "<b>Let op: Indien u vragen heeft, onthoudt u dan het artikelnr " + data.getNumber() + ".</b><b?";
Thread.sleep(150);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("tinyMCE.activeEditor.setContent('" + id + nr + message + description + "<br>" + id + "')");
Thread.sleep(150);
// ***********************************************************************************************
WebElement ui_price = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("price.value")));
ui_price.clear();
ui_price.sendKeys(data.getPrice().toString());
// ***********************************************************************************************
WebElement ui_confirm = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("syi-place-ad-button")));
ui_confirm.click();
// ***********************************************************************************************
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("msg-success-flagging")));
}
}
db.updateTurns(customers.getId());
}
Any advice is welcome.