问题
I have 20 pages and each page have 2 testcases and each testcase download a number of files. I want to change the download directory for each test case at runtime.
Here is the 'TestBaseClass' code which downloading all the files in one particular folder from where I have to separate them as per category and place them into a particular folder. There are 20 folders and each folder is having 2 subfolders 'ChapterLevel' & 'PracticeLevel' in which I do have to place it manually.
Is it possible to change the download directory by passing a variable during runtime?
My TestBaseClass code:
public static WebDriver driver;
public static void initialization() throws InvocationTargetException {
try {
// Setting new download directory path
Map<String, Object> prefs = new HashMap<String, Object>();
// Use File.separator as it will work on any OS
prefs.put("download.default_directory", "C:\\Users\\pd\\Desktop\\AHNPTTest");
// Adding cpabilities to ChromeOptions
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
// Launching browser with desired capabilities
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options);
}
catch (Exception e) {
// generic exception handling
e.printStackTrace();
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
}
Here is my testcase:
public class ANA_TC16_RiskAnalysisNewTest extends TestBaseClass {
ANA_RiskAnalysisNewPage New;
@BeforeMethod
public void setUp() {
try {
initialization();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
login();
New = new ANA_RiskAnalysisNewPage();
}
@Test
public void chapterrLevelTest() throws Exception
{
New.hoverTest();
New.clickBottomOptions();
New.chapterOption();
New.TopX();
New.ATISlider();
New.conditionSelection();
New.takeScreenshot("Risk Analysis New Chapter Level Image");
New.downloadOptions();
New.isFileDownloaded();
}
@Test
public void practiceLevelTest() throws Exception
{
New.hoverTest();
New.clickBottomOptions();
New.providerOption();
New.TopX();
New.ATISlider();
New.conditionSelection();
New.takeScreenshot("Risk Analysis New Practice Level Image");
New.downloadOptions();
New.isFileDownloaded();
}
}
回答1:
Suppose you want to specify download folder for each test method.
- Add parameter for
downloadPath
ininitialization
inTestBaseClass
. - Add parameter for
downloadPath
insetup
inANA_TC16_RiskAnalysisNewTest
, remove the@BerforMethod
annotation and update each test method to callsetup
in begin with desireddownloadPath
.
public class TestBaseClass {
public static void initialization(String downloadPath) throws InvocationTargetException {
try {
// Setting new download directory path
Map<String, Object> prefs = new HashMap<String, Object>();
// Use File.separator as it will work on any OS
prefs.put("download.default_directory", downloadPath);
...
public class ANA_TC16_RiskAnalysisNewTest extends TestBaseClass {
ANA_RiskAnalysisNewPage New;
// @BeforeMethod
public void setUp(String downloadPath) {
try {
initialization(downloadPath);
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
login();
New = new ANA_RiskAnalysisNewPage();
}
@Test
public void chapterrLevelTest() throws Exception {
setUp("C:\\Users\\pd\\Desktop\\AHNPTTest\\ANA_TC16_RiskAnalysis\\ChapterLevel");
New.hoverTest();
...
}
@Test
public void practiceLevelTest() throws Exception {
setUp("C:\\Users\\pd\\Desktop\\AHNPTTest\\ANA_TC16_RiskAnalysis\\PracticeLevel");
New.hoverTest();
...
}
...
来源:https://stackoverflow.com/questions/62779586/how-do-i-change-the-download-path-dynamically-during-runtime-using-chrome-in-sel