Best Testsigma code snippet using com.testsigma.service.ElementService.export
Source:ElementService.java
...7 *8 */9package com.testsigma.service;10import com.testsigma.dto.BackupDTO;11import com.testsigma.dto.export.ElementXMLDTO;12import com.testsigma.event.ElementEvent;13import com.testsigma.event.EventType;14import com.testsigma.exception.ResourceNotFoundException;15import com.testsigma.mapper.ElementMapper;16import com.testsigma.model.*;17import com.testsigma.repository.ElementRepository;18import com.testsigma.specification.ElementSpecificationsBuilder;19import com.testsigma.specification.SearchCriteria;20import com.testsigma.specification.SearchOperation;21import com.testsigma.specification.TestCaseSpecificationsBuilder;22import com.testsigma.web.request.ElementRequest;23import com.testsigma.web.request.ElementScreenNameRequest;24import lombok.RequiredArgsConstructor;25import lombok.extern.log4j.Log4j2;26import org.springframework.beans.factory.annotation.Autowired;27import org.springframework.context.ApplicationEventPublisher;28import org.springframework.dao.DataIntegrityViolationException;29import org.springframework.data.domain.Page;30import org.springframework.data.domain.PageRequest;31import org.springframework.data.domain.Pageable;32import org.springframework.data.jpa.domain.Specification;33import org.springframework.stereotype.Service;34import java.io.IOException;35import java.util.ArrayList;36import java.util.Arrays;37import java.util.List;38import java.util.Objects;39@Service("elementService")40@Log4j241@RequiredArgsConstructor(onConstructor = @__(@Autowired))42public class ElementService extends XMLExportService<Element> {43 private final ElementRepository elementRepository;44 private final TestCaseService testCaseService;45 private final TagService tagService;46 private final TestStepService testStepService;47 private final ElementMapper elementMapper;48 private final ElementScreenService screenNameService;49 private final ApplicationEventPublisher applicationEventPublisher;50 public List<Element> findByNameInAndWorkspaceVersionId(List<String> elementNames, Long workspaceVersionId) {51 return elementRepository.findByNameInAndWorkspaceVersionId(elementNames, workspaceVersionId);52 }53 public Element findByNameAndVersionId(String name, Long versionId) {54 return elementRepository.findFirstElementByNameAndWorkspaceVersionId(name,55 versionId);56 }57 public Element find(Long id) throws ResourceNotFoundException {58 return elementRepository.findById(id)59 .orElseThrow(() -> new ResourceNotFoundException("Element is not found with id: " + id));60 }61 public Page<Element> findAll(Specification<Element> specification, Pageable pageable) {62 return elementRepository.findAll(specification, pageable);63 }64 public Element save(Element element) {65 return elementRepository.save(element);66 }67 public Element create(Element element) {68 element = this.save(element);69 this.markAsDuplicated(element);70 publishEvent(element, EventType.CREATE);71 return element;72 }73 public Element update(Element element, String oldName, String previousLocatorValue, LocatorType previousLocatorType, Long previousScreenNameId)74 throws ResourceNotFoundException {75 element = this.save(element);76 if (!Objects.equals(element.getLocatorValue(), previousLocatorValue) || element.getLocatorType() != previousLocatorType77 || !Objects.equals(element.getScreenNameId(), previousScreenNameId)) {78 this.markAsDuplicated(element);79 this.resetDuplicate(element.getWorkspaceVersionId(), previousLocatorValue, previousLocatorType, previousScreenNameId);80 }81 this.eventCallForUpdate(oldName, element);82 return element;83 }84 public Element update(Element element, String oldName) {85 element = this.save(element);86 this.eventCallForUpdate(oldName, element);87 return element;88 }89 public void delete(Element element) {90 elementRepository.delete(element);91 this.resetDuplicate(element.getWorkspaceVersionId(), element.getLocatorValue(), element.getLocatorType(), element.getScreenNameId());92 publishEvent(element, EventType.DELETE);93 }94 public void bulkDelete(Long[] ids, Long workspaceVersionId) throws Exception {95 Boolean allIdsDeleted = true;96 TestCaseSpecificationsBuilder builder = new TestCaseSpecificationsBuilder();97 for (Long id : ids) {98 List<SearchCriteria> params = new ArrayList<>();99 Element element = this.find(id);100 params.add(new SearchCriteria("element", SearchOperation.EQUALITY, element.getName()));101 params.add(new SearchCriteria("workspaceVersionId", SearchOperation.EQUALITY, workspaceVersionId));102 builder.setParams(params);103 Specification<TestCase> spec = builder.build();104 Page<TestCase> linkedTestCases = testCaseService.findAll(spec, PageRequest.of(0, 1));105 if (linkedTestCases.getTotalElements() == 0) {106 this.delete(element);107 } else {108 allIdsDeleted = false;109 }110 }111 if (!allIdsDeleted) {112 throw new DataIntegrityViolationException("dataIntegrityViolationException: Failed to delete some of the Elements " +113 "since they are already associated to some Test Cases.");114 }115 }116 public void bulkUpdateScreenNameAndTags(Long[] ids, String screenName, String[] tags) throws ResourceNotFoundException {117 for (Long id : ids) {118 Element element = find(id);119 if (screenName.length() > 0) {120 ElementScreenNameRequest elementScreenNameRequest = new ElementScreenNameRequest();121 elementScreenNameRequest.setName(screenName);122 elementScreenNameRequest.setWorkspaceVersionId(element.getWorkspaceVersionId());123 ElementScreenName elementScreenName = screenNameService.save(elementScreenNameRequest);124 element.setScreenNameId(elementScreenName.getId());125 }126 update(element, element.getName(), element.getLocatorValue(), element.getLocatorType(), element.getScreenNameId());127 tagService.updateTags(Arrays.asList(tags), TagType.ELEMENT, id);128 }129 }130 public void updateByName(String name, ElementRequest elementRequest) {131 Element element = findByNameAndVersionId(name, elementRequest.getWorkspaceVersionId());132 String oldName = element.getName();133 elementMapper.merge(elementRequest, element);134 update(element, oldName);135 }136 public void publishEvent(Element element, EventType eventType) {137 ElementEvent<Element> event = createEvent(element, eventType);138 log.info("Publishing event - " + event.toString());139 applicationEventPublisher.publishEvent(event);140 }141 public ElementEvent<Element> createEvent(Element element, EventType eventType) {142 ElementEvent<Element> event = new ElementEvent<>();143 event.setEventData(element);144 event.setEventType(eventType);145 return event;146 }147 public void export(BackupDTO backupDTO) throws IOException, ResourceNotFoundException {148 if (!backupDTO.getIsElementEnabled()) return;149 log.debug("backup process for element initiated");150 writeXML("elements", backupDTO, PageRequest.of(0, 25));151 log.debug("backup process for element completed");152 }153 public Specification<Element> getExportXmlSpecification(BackupDTO backupDTO) {154 SearchCriteria criteria = new SearchCriteria("workspaceVersionId", SearchOperation.EQUALITY, backupDTO.getWorkspaceVersionId());155 List<SearchCriteria> params = new ArrayList<>();156 params.add(criteria);157 ElementSpecificationsBuilder elementSpecificationsBuilder = new ElementSpecificationsBuilder();158 elementSpecificationsBuilder.params = params;159 return elementSpecificationsBuilder.build();160 }161 @Override...
Source:BackupDetailService.java
...8import com.testsigma.config.StorageServiceFactory;9import com.testsigma.model.StorageAccessLevel;10import com.testsigma.constants.MessageConstants;11import com.testsigma.dto.BackupDTO;12import com.testsigma.dto.export.BaseXMLDTO;13import com.testsigma.exception.ResourceNotFoundException;14import com.testsigma.exception.TestsigmaException;15import com.testsigma.mapper.BackupDetailMapper;16import com.testsigma.model.BackupDetail;17import com.testsigma.model.BackupStatus;18import com.testsigma.repository.BackupDetailRepository;19import com.testsigma.web.request.BackupRequest;20import lombok.RequiredArgsConstructor;21import lombok.extern.log4j.Log4j2;22import org.springframework.beans.factory.annotation.Autowired;23import org.springframework.data.domain.Page;24import org.springframework.data.domain.Pageable;25import org.springframework.data.jpa.domain.Specification;26import org.springframework.stereotype.Service;27import java.io.IOException;28import java.net.URL;29import java.sql.Timestamp;30import java.util.List;31import java.util.Optional;32@Log4j233@Service34@RequiredArgsConstructor(onConstructor = @__({@Autowired}))35public class BackupDetailService extends XMLExportService<BackupDetail> {36 private final BackupDetailRepository repository;37 private final StorageServiceFactory storageServiceFactory;38 private final AgentService agentService;39 private final WorkspaceService workspaceService;40 private final AttachmentService attachmentService;41 private final TestDeviceService testDeviceService;42 private final TestPlanService testPlanService;43 private final RestStepService reststepService;44 private final TestCaseService testcaseService;45 private final TestCasePriorityService testCasePriorityService;46 private final TestCaseTypeService testCaseTypeService;47 private final TestDataProfileService testDataProfileService;48 private final TestStepService teststepService;49 private final ElementService elementService;50 private final WorkspaceVersionService versionService;51 private final ElementScreenService elementScreenService;52 private final UploadService uploadService;53 private final UploadVersionService uploadVersionService;54 private final BackupDetailMapper exportBackupEntityMapper;55 public BackupDetail find(Long id) throws ResourceNotFoundException {56 return repository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Backup is not found with id:" + id));57 }58 public Page<BackupDetail> findAll(Pageable pageable) {59 return repository.findAll(pageable);60 }61 public Optional<URL> downLoadURL(BackupDetail backupDetail) {62 return storageServiceFactory.getStorageService().generatePreSignedURLIfExists(63 "/backup/" + backupDetail.getName(), StorageAccessLevel.READ, 300);64 }65 public BackupDetail create(BackupDetail backupDetail) {66 backupDetail.setMessage(MessageConstants.BACKUP_IS_IN_PROGRESS);67 backupDetail.setStatus(BackupStatus.IN_PROGRESS);68 backupDetail.setCreatedDate(new Timestamp(System.currentTimeMillis()));69 backupDetail = this.repository.save(backupDetail);70 return backupDetail;71 }72 public BackupDetail save(BackupDetail backupDetail) {73 return this.repository.save(backupDetail);74 }75 public void destroy(Long id) throws ResourceNotFoundException {76 BackupDetail detail = this.find(id);77 this.repository.delete(detail);78 }79 @Override80 protected Page<BackupDetail> findAll(Specification<BackupDetail> specification, Pageable pageRequest) throws ResourceNotFoundException {81 return null;82 }83 @Override84 protected List<? extends BaseXMLDTO> mapToXMLDTOList(List<BackupDetail> list) {85 return null;86 }87 @Override88 public Specification<BackupDetail> getExportXmlSpecification(BackupDTO backupDTO) throws ResourceNotFoundException {89 return null;90 }91 public void export(BackupRequest request) throws IOException, TestsigmaException {92 BackupDTO backupDTORequest = exportBackupEntityMapper.map(request);93 BackupDetail backupDetailRequest = exportBackupEntityMapper.map(backupDTORequest);94 final BackupDetail backupDetail = create(backupDetailRequest);95 final BackupDTO backupDTO = exportBackupEntityMapper.mapTo(backupDetail);96 log.debug("initiating backup - " + backupDetail.getId());97 new Thread(() -> {98 try {99 log.debug("backup process started for ::" + backupDetail.getId());100 initExportFolder(backupDTO);101 workspaceService.export(backupDTO);102 versionService.export(backupDTO);103 testCasePriorityService.export(backupDTO);104 testCaseTypeService.export(backupDTO);105 elementScreenService.export(backupDTO);106 elementService.export(backupDTO);107 testDataProfileService.export(backupDTO);108 attachmentService.export(backupDTO);109 agentService.export(backupDTO);110 uploadService.export(backupDTO);111 uploadVersionService.export(backupDTO);112 testcaseService.export(backupDTO);113 teststepService.export(backupDTO);114 reststepService.export(backupDTO);115 testPlanService.export(backupDTO);116 testDeviceService.export(backupDTO);117 backupDetail.setSrcFiles(backupDTO.getSrcFiles());118 backupDetail.setDestFiles(backupDTO.getDestFiles());119 exportToStorage(backupDetail);120 log.debug("backup process export completed");121 } catch (Exception e) {122 log.error(e.getMessage(), e);123 backupDetail.setStatus(BackupStatus.FAILURE);124 backupDetail.setMessage(e.getMessage());125 repository.save(backupDetail);126 destroy(backupDTO);127 } catch (Error error) {128 log.error(error.getMessage(), error);129 } finally {130 destroy(backupDTO);131 log.debug("backup process for completed");132 }133 }).start();134 }...
export
Using AI Code Generation
1import com.testsigma.service.ElementService;2import com.testsigma.service.ElementServiceFactory;3import com.testsigma.service.ElementServiceFactoryImpl;4import com.testsigma.service.ElementServiceType;5public class 2 {6public static void main(String[] args) {7ElementServiceFactory elementServiceFactory = new ElementServiceFactoryImpl();
export
Using AI Code Generation
1import java.io.File;2import java.io.IOException;3import java.net.MalformedURLException;4import java.net.URL;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.remote.RemoteWebDriver;8import com.testsigma.service.ElementService;9import com.testsigma.service.ElementServiceFactory;10public class TestExportElement {11 public static void main(String[] args) throws MalformedURLException, IOException {12 WebElement element = driver.findElement(null);13 ElementService elementService = ElementServiceFactory.getElementService();14 File file = elementService.exportElement(element, "png");15 System.out.println(file.getAbsolutePath());16 }17}18import java.io.File;19import java.io.IOException;20import java.net.MalformedURLException;21import java.net.URL;22import org.openqa.selenium.WebDriver;23import org.openqa.selenium.WebElement;24import org.openqa.selenium.remote.RemoteWebDriver;25import com.testsigma.service.ElementService;26import com.testsigma.service.ElementServiceFactory;27public class TestExportElement {28 public static void main(String[] args) throws MalformedURLException, IOException {29 WebElement element = driver.findElement(null);30 ElementService elementService = ElementServiceFactory.getElementService();31 File file = elementService.exportElement(element, "jpeg");32 System.out.println(file.getAbsolutePath());33 }34}35import java.io.File;36import java.io.IOException;37import java.net.MalformedURLException;38import java.net.URL;39import org.openqa.selenium.WebDriver;40import org.openqa.selenium.WebElement;41import org.openqa.selenium.remote.RemoteWebDriver;42import com.testsigma.service.ElementService;43import com.testsigma.service.ElementServiceFactory;
export
Using AI Code Generation
1import com.testsigma.service.ElementService;2import com.testsigma.service.ElementServiceFactory;3import com.testsigma.service.ElementServiceException;4public class 2 {5 public static void main(String[] args) {6 try {7 ElementService es = ElementServiceFactory.getElementService();8 es.export("C:\\elementinfo.txt");9 } catch (ElementServiceException e) {10 e.printStackTrace();11 }12 }13}14import com.testsigma.service.ElementService;15import com.testsigma.service.ElementServiceFactory;16import com.testsigma.service.ElementServiceException;17public class 3 {18 public static void main(String[] args) {19 try {20 ElementService es = ElementServiceFactory.getElementService();21 es.import("C:\\elementinfo.txt");22 } catch (ElementServiceException e) {23 e.printStackTrace();24 }25 }26}27import com.testsigma.service.ElementService;28import com.testsigma.service.ElementServiceFactory;29import com.testsigma.service.ElementServiceException;30import com.testsigma.service.ElementInfo;31public class 4 {32 public static void main(String[] args) {33 try {34 ElementService es = ElementServiceFactory.getElementService();35 ElementInfo ei = es.getElement("C:\\elementinfo.txt");36 System.out.println("Element Name: " + ei.getName());37 System.out.println("Element Type: " + ei.getType());38 System.out.println("Element Value: " + ei.getValue());39 } catch (ElementServiceException e) {40 e.printStackTrace();41 }42 }43}44import com.testsigma.service.ElementService;45import com.testsigma.service.ElementServiceFactory;46import com.testsigma.service.ElementServiceException;47import com.testsigma.service.ElementInfo;48import java.util.List;49public class 5 {50 public static void main(String[] args) {51 try {52 ElementService es = ElementServiceFactory.getElementService();53 List<ElementInfo> eilist = es.getElements("C:\\elementinfo.txt");
export
Using AI Code Generation
1package com.testsigma.testscripts;2import java.io.File;3import java.io.IOException;4import java.net.MalformedURLException;5import java.net.URL;6import java.util.concurrent.TimeUnit;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.remote.DesiredCapabilities;9import org.openqa.selenium.remote.RemoteWebDriver;10import org.testng.annotations.AfterTest;11import org.testng.annotations.BeforeTest;12import org.testng.annotations.Test;13import com.testsigma.service.ElementService;14public class ExportElementTest {15private WebDriver driver;16private ElementService elementService;17public void setUp() throws MalformedURLException {18DesiredCapabilities capabilities = new DesiredCapabilities();19capabilities.setCapability("deviceName", "Android Emulator");20capabilities.setCapability("platformName", "Android");21capabilities.setCapability("platformVersion", "4.4.2");22capabilities.setCapability("appPackage", "com.android.calculator2");23capabilities.setCapability("appActivity", ".Calculator");
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!