简体   繁体   中英

How to upload multiple files/images using Robot class in selenium Webdriver? (windows) Now want to upload 2nd Image? Please help me to Reuses samecode

package TestCases;

import org.testng.annotations.Test;
    
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

import Common_library.ExcelLibrary;
import Common_library.SuperTestScript;
import Module_Scripts.Assignments;
import Module_Scripts.HomePage;
import Module_Scripts.Loginpage;

public class Login_TC1 extends SuperTestScript
{
    @Test
    public void  LoginTC1() throws Exception
    {
        //all the required data
        
                String USRID = ExcelLibrary.readData("Sheet1", 0, 0);//all the required data
                String PSW = ExcelLibrary.readData("Sheet1", 0, 1);//all the required data

        //create page objects
                Loginpage lp = new Loginpage();//create page objects
                HomePage Hp = new HomePage();
                Assignments As = new Assignments();
                Robot robot = new Robot();

        //invoke the methods
                lp.enterUserid(USRID);//invoke the methods
                lp.enterPSW(PSW);//invoke the methods
                lp.clickOnSigninButton();//invoke the methods
                Hp.ClickonAssignments();//invoke the methods//invoke the methods
                As.ClickOnDuedatepassed();//invoke the methods
                As.ClickOnSubmission();//invoke the methods
                As.ClickOnUploadButton();//invoke the methods
                As.ClickOnImages();//invoke the methods
                //Robot class
                Thread.sleep(300);
                StringSelection str = new StringSelection("C:\\Users\\DGM\\Desktop\\bug images\\1.PNG");
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(str, null);
                robot.keyPress(KeyEvent.VK_CONTROL);
                robot.keyPress(KeyEvent.VK_V);
                Thread.sleep(3000);
                robot.keyRelease(KeyEvent.VK_CONTROL);
                robot.keyRelease(KeyEvent.VK_V);
                robot.keyPress(KeyEvent.VK_ENTER);
                robot.keyRelease(KeyEvent.VK_ENTER);

Thread.sleep(300); StringSelection str = new StringSelection("C:\\Users\\DGM\\Desktop\\bug images\\2.PNG"); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(str, null); robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_V); Thread.sleep(3000); robot.keyRelease(KeyEvent.VK_CONTROL); robot.keyRelease(KeyEvent.VK_V); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); } }

If you know how many images needs to uploaded then define a count for that then use a loop.

class Utility{
       public void uploadImage(){
            Thread.sleep(300);
            StringSelection str = new StringSelection("C:\\Users\\DGM\\Desktop\\bug images\\1.PNG");
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(str, null);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            Thread.sleep(3000);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);

            Thread.sleep(300);
            StringSelection str = new StringSelection("C:\\Users\\DGM\\Desktop\\bug images\\2.PNG");
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(str, null);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            Thread.sleep(3000);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_V);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
}
}

Call the uploadImage method into your test.

    int images = 2;
    for (int i = 0; i < images; i++) {
     Utility utility = new Utility();
     utility.uploadImage();
      }

If the images count is dynamic and needs to be passed manually then read it from excel or text file before your @Test

    int images = readImageCountFromDataSheet();
    for (int i = 0; i < images; i++) {
     Utility utility = new Utility();
     utility.uploadImage();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM